Opened 10 years ago

Closed 10 years ago

#23236 closed New feature (wontfix)

forms.Form should support a Meta class like forms.ModelForm

Reported by: django@… Owned by: nobody
Component: Forms Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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)

Change History (3)

comment:1 by Tim Graham, 10 years ago

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

comment:2 by Baptiste Mispelon, 10 years ago

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.

comment:3 by Tim Graham, 10 years ago

Resolution: wontfix
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top