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 __ini开发者_运维百科t__
, 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.
精彩评论