简体   繁体   中英

Django model formsets - modifying form labels and defaults

I'm building a formset like so:

InterestFormSet = modelformset_factory(Interest, \
    formset=BaseInterestFormSet, exclude=('userid',), extra=2) 

And I want set default labels and values for elements of this form.

I know that in simple forms I can use the fields dict to change these things for specific fields of the form, but how is this done with a formset?

I tried extending the formset (as you can see) to see if I could access self.fields from within __init__ , but no luck.

Something like this should do what you want:

class InterestForm(ModelForm):
    pub_date = DateField(label='Publication date')

    class Meta:
        model = Interest
        exclude = ('userid',)


InterestFormSet = modelformset_factory(Interest, form=InterestForm, extra=2)

Formsets don't have fields, they only have forms which have fields. So you have to deal directly with those forms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM