#25408: Pass additional arguments to BaseForm.__init__ from 
BaseModelForm.__init__
-------------------------------------+-------------------------------------
     Reporter:  MoritzS              |                    Owner:  nobody
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    Component:  Forms                |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:  form modelform args  |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by MoritzS):

 The reason you need to inherit it from `BaseForm` becomes more obvious
 when you look at the mro of the forms. Suppose you have a `UserForm`.
 Normally its mro will look like this:
 {{{
 UserForm
 django.forms.forms.BaseForm
 object
 }}}
 Now you want to add functionality to all forms in your project so you
 create a new mixin called `ProjectFormMixin`. If you then have a new
 `MyProjectBaseForm` like this:
 {{{
 class MyProjectBaseForm(ProjectFormMixin, BaseForm):
     def has_changed(self):
         # custom detection if form has changed that also calls
         # super().has_changed() at some point
 }}}
 And `UserForm`'s mro (if you inherit it from `MyProjectBaseForm`) will now
 be:
 {{{
 UserForm
 MyProjectBaseForm
 ProjectFormMixin
 django.forms.forms.BaseForm
 object
 }}}
 However if you also want to use this mixin in a model form base like this:
 {{{
 class MyProjectBaseModelForm(ProjectFormMixin, BaseModelForm):
     pass
 }}}
 the mro of a `UserModelForm` will be:
 {{{
 UserModelForm
 MyProjectBaseModelForm
 ProjectFormMixin
 django.forms.models.BaseModelForm
 django.forms.forms.BaseForm
 object
 }}}
 when instead you wanted `ProjectFormMixin` to be right above
 `django.forms.forms.BaseForm`.
 That's because `super()` calls inside the `ProjectFormMixin` will then hit
 `BaseForm` as intended instead of `BaseModelForm`.
 Also `super()` calls form within `BaseModelForm` (they only relevant one
 is in `BaseModelForm.__init__()`) will then hit your `ProjectFormMixin`
 instead of the other way around.

--
Ticket URL: <https://code.djangoproject.com/ticket/25408#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.a4d5f7f3a060cd6d0557753bf3819d16%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to