Django - Adding fields to a ModelForm that inherits from a parent modelform?

Viewed 533

ex. model = Foo Foo.fields(a,b,c,d)

FooModelForm1(forms.Modelform): fields = [a, b, c] etc.

FooModelForm2(FooModelForm1): fields = [d]

As you can see all 4 fields exist in the model but FooModelForm1 is using only 3 fields while i want FooModelForm2 to include field [d] as well as the others.

In the documentation it explains you can exclude fields so I know one option would be to flip my forms and include field [d] initially but I was curious if the opposite was possible.

I have seen responses that modifies the Meta data from FooModelForm2 but that doesnt seem to be working for me such as:

class Meta(FooModelForm1.Meta):
        fields = ReqLineForm.Meta.fields + ['d'] 

Solved

I made a mistake switching the form I was calling in my view to the new form I created that inherited my old form. So adding a field is as easy as what I put above the update dependent if you used a list or tuple to set your fields.

1 Answers

Something like this ...

class FooModelForm2(FooModelForm1):
     field_d = forms.EmailField(required=True)

    class Meta:
        model = User
        fields = ("field_a", "field_b", "field_c", "field_d")
Related