开发者

Django model formsets - modifying form labels and defaults

开发者 https://www.devze.com 2023-01-23 16:04 出处:网络
I\'m building a formset like so: InterestFormSet = modelformset_factory(Interest, \\ formset=BaseInterestFormSet, exclude=(\'userid\',), extra=2)

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消