[Django] #23236: forms.Form should support a Meta class like forms.ModelForm

30 views
Skip to first unread message

Django

unread,
Aug 6, 2014, 5:53:02 AM8/6/14
to django-...@googlegroups.com
#23236: forms.Form should support a Meta class like forms.ModelForm
-----------------------------+--------------------
Reporter: django@… | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------
Please feel free to close this if you think my use case is too specific.

My use case is that I'd like to edit the `widgets` of a subclassed
form.Form, the only way I can currently do this is by overriding the
entire field or by overriding `__init__()`

Example:

{{{
class MyAuthenticationForm(django.contrib.auth.forms.AuthenticationForm):
class Meta:
wigets = {
'username' : MyFancyWidget()
}
}}}

Right now, I have to do something like (which isn't especially DRY):


{{{
class MyAuthenticationForm(django.contrib.auth.forms.AuthenticationForm):
def __init__(self, *args, **kwargs):
super(MyAuthenticationForm, self).__init__(self, *args, **kwargs)
self.fields['username'].widget = MyFancyWidget()
}}}


I argue that a form's `Meta` class will be a familiar concept to most
django developers (as `ModelForms` are commonly used), and so extending to
`forms.Form` would be natural.

Of course, if `Meta` were used for plain old `forms.Form`, then you could
also use it as a means to exclude fields in the parent form (e.g. in this
case you could do something like


{{{
class Meta:
exclude = ('password',)
}}}


(not that that would be any use in this case)

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

Django

unread,
Aug 6, 2014, 6:52:13 AM8/6/14
to django-...@googlegroups.com
#23236: forms.Form should support a Meta class like forms.ModelForm
-----------------------------+--------------------------------------

Reporter: django@… | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------
Changes (by timo):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

`ModelFormMetaclass` is very complex. I don't think adding it to `Form` is
a good idea.

--
Ticket URL: <https://code.djangoproject.com/ticket/23236#comment:1>

Django

unread,
Aug 9, 2014, 1:27:35 PM8/9/14
to django-...@googlegroups.com
#23236: forms.Form should support a Meta class like forms.ModelForm
-----------------------------+--------------------------------------

Reporter: django@… | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------

Comment (by bmispelon):

I don't see what's "un-DRY" about extending the `__init__()` method and
setting up the widget there.

I like the plain `Form`'s (relative) simplicity and lack of a "magic"
`Meta` class.
Overriding things in `__init__()` is the normal Python way of doing
things.

All-in-all, I'm -0 on the idea.

--
Ticket URL: <https://code.djangoproject.com/ticket/23236#comment:2>

Django

unread,
Aug 12, 2014, 7:25:38 PM8/12/14
to django-...@googlegroups.com
#23236: forms.Form should support a Meta class like forms.ModelForm
-----------------------------+--------------------------------------
Reporter: django@… | Owner: nobody
Type: New feature | Status: closed
Component: Forms | Version: master
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------
Changes (by timgraham):

* status: new => closed
* resolution: => wontfix


--
Ticket URL: <https://code.djangoproject.com/ticket/23236#comment:3>

Reply all
Reply to author
Forward
0 new messages