Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django ModelForm inheritance and Meta inheritance

I have this ModelForm:

class Event(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(Event, self).__init__(*args, **kwargs)
        ##Here make some changes such as:
        self.helper = FormHelper()
        self.helper.form_method = 'POST'
        ##Many settings here which **i don't want to rewrite in 10 child classes**

    class Meta:
        model = Event
        exclude = something...
        widgets = some settings here also.

And this child ModelForm:

class UpgradedEvent(Event):

    def __init__(self, *args, **kwargs):
        super(UpgradedEvent,self).__init__(*args,**kwargs)

    class Meta(Event.Meta):
        model = UpgradedEvent

UpgradedEvent is a child of Event model but has some extra fields. How can i inherit all the settings from the Event FORM into UpgradedEvent FORM?

When running the above code, it renders the Event form. Is there a way to inherit only the settings inside __init__ ?

EDIT

Check out the answer, it works great but keep in mind: you need to create another instance of FormHelper in your child class, otherwise it won't work. So child class should look something like:

class UpgradedEvent(Event):

    def __init__(self, *args, **kwargs):
        super(UpgradedEvent,self).__init__(*args,**kwargs)
        self.helper = FormHelper()

    class Meta(Event.Meta):
        model = UpgradedEvent
like image 561
Mike Vlad Avatar asked May 23 '18 11:05

Mike Vlad


2 Answers

You can obtain the fields the Meta above, and extend the lists, etc.:

class UpgradedEventForm(EventForm):

    def __init__(self, *args, **kwargs):
        super(UpgradedEventForm,self).__init__(*args,**kwargs)
        # some extra settings
        # ...
        # for example
        self.fields['extra_field'].initial = 'initial value of extra field'

    class Meta(EventForm.Meta):
        model = UpgradedEvent
        exclude = EventForm.Meta.exclude + ['extra_exclude1', 'extra_exclude2']
        fields = EventForm.Meta.fields + ['extra_field']

So by using inheritance, we can add extra procedures to the __init__ function by performing some extra actions after the super(UpgradedEventForm, self) call, and wwe can access the attributes of our parent, and extend these.

Note that you better name your forms with a Form suffix, since now your models clash with your forms. As a result, your Form seems to have as model a reference to the Form itself. By using proper "nomenclature", you avoid a lot of mistakes.

like image 199
Willem Van Onsem Avatar answered Sep 23 '22 00:09

Willem Van Onsem


Create FormWithSettings which will hold common settings for you form classes and inherit it

class FormWithSettings(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(FormWithSettings, self).__init__(*args, **kwargs)
        ##Here make some changes such as:
        self.helper = FormHelper()
        self.helper.form_method = 'POST'
        ##Many settings here which **i don't want to rewrite in 10 child classes**

    class Meta:
        exclude = something...
        widgets = some settings here also.

class EventForm(FormWithSettings):

    def __init__(self, *args, **kwargs):
        super(EventForm, self).__init__(*args,**kwargs)

    class Meta(FormWithSettings.Meta):
        model = Event

class UpgradedEventForm(FormWithSettings):

    def __init__(self, *args, **kwargs):
        super(UpgradedEventForm, self).__init__(*args,**kwargs)

    class Meta(FormWithSettings.Meta):
        model = UpgradedEvent
like image 33
Sardorbek Imomaliev Avatar answered Sep 25 '22 00:09

Sardorbek Imomaliev