ContactForm
is a common form in my app being used multiple times. I already have a Foreign Key
of Contact
Model in Guardian
table.So the accepted answer from the solution works for me.
But its making bit complex to write entire form again for below ContactForm
fields when I have to reuse. So kept ContactForm
separate to make it reusable.
But still sometimes few fields are optional from the ContactForm
, so not able to reuse same ContactForm
again.
class ContactForm(forms.ModelForm):
class Meta:
model = Contact
fields = ("address", "city", "district", "state", "country",
"pincode", "phone1", "phone2", "is_emergency")
Ex. From above ContactForm
, I want to use only phone1
, phone2
and is_emergency
flag in the GuardianForm
.
Is there anyway to override Meta
class by inheriting ContactForm
in to the GuardianForm
such as:
class GuardianForm(forms.ModelForm, ContactForm): #obviously this will not work
first_name = forms.CharField(max_length=30, required=False, label = "First Name")
last_name = forms.CharField(max_length=30, required=False, label = "Last Name")
# Other Fields ...
class Meta:
model = Guardian
fields = ('passport_number', 'pan_number', 'annual_income',
'phone1', 'phone2', 'is_emergency',) # Only 3 Fields of `ContactForm`
OR - Is there any standard better approach to overcome this issue?
I am not sure what you are trying to achive, but here is how you can extend Django Forms's Meta Class :
class GuardianForm(ContactForm):
class Meta(ContactForm.Meta):
fields = ("phone1", "phone2", "is_emergency")