Opened 14 years ago

Closed 12 years ago

#13769 closed New feature (wontfix)

ModelForm: override default field attributes

Reported by: Rolando Espinoza La fuente Owned by: asenchi
Component: Forms Version: dev
Severity: Normal Keywords: modelform fields_attrs
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The common way to alter fields attributes seems to be overriding ModelForm __init__, like:

class FooForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(FooForm, self).__init__(*args, **kwargs)
        self['field'].attr = "foo"

In #11925 was added the widget Meta attribute. In the same way could be provided a mechanism to override default field attributes.

The patch attached adds support for Meta.fields_attrs. For example:

class Foo(models.Model):
    user = models.ForeignKey(User)

class FooForm(forms.ModelForm):
    """Renders a select widget with "-----" as first option and display all users"""
    class Meta:
        model = Foo

class StaffFooForm(forms.ModelForm):
    """Renders a select widget without empty option and only display staff users"""
    class Meta:
        model = Foo
        fields_attrs = {
            'user': {
                'empty_label': None,
                'queryset': User.objects.filter(is_staff=True),
            },
        }

Attachments (3)

formmodel_fields_attrs_r13353.patch (2.2 KB ) - added by Rolando Espinoza La fuente 14 years ago.
fields-kwargs.diff (4.6 KB ) - added by Petr Marhoun <petr.marhoun@…> 14 years ago.
fields-kwargs.2.diff (4.5 KB ) - added by Petr Marhoun <petr.marhoun@…> 14 years ago.

Download all attachments as: .zip

Change History (13)

by Rolando Espinoza La fuente, 14 years ago

by Petr Marhoun <petr.marhoun@…>, 14 years ago

Attachment: fields-kwargs.diff added

comment:1 by Petr Marhoun <petr.marhoun@…>, 14 years ago

Added another patch for the same feature - I implemented it independently (and found this ticket later). My patch has tests and use different name for proposed feature (fields_kwargs instead fields_attrs - but it is not important for me and I do not know what is better).

There is also some motivation here:
http://code.djangoproject.com/wiki/ImprovementsForDjangoForms#Modelfieldswithnot-defaultvalues

I think also there is a problem in the original patch - function should not have dictionary as default argument because dictionary is mutable.

comment:2 by asenchi, 14 years ago

Owner: changed from nobody to asenchi

comment:3 by asenchi, 14 years ago

Needs documentation: set
Patch needs improvement: set
Triage Stage: UnreviewedDesign decision needed

Tested 'fields-kwargs.diff', patch applied cleanly all tests ran. Test should be rewritten into unittests. Also needs documentation.

comment:4 by asenchi, 14 years ago

Cc: Rolando Espinoza La fuente removed
Needs documentation: unset
Patch needs improvement: unset
Resolution: fixed
Status: newclosed

by Petr Marhoun <petr.marhoun@…>, 14 years ago

Attachment: fields-kwargs.2.diff added

comment:5 by Petr Marhoun <petr.marhoun@…>, 14 years ago

Resolution: fixed
Status: closedreopened

Ticket reopened: New meta attribute "widgets" was really implemented in 1.2, but it is only for overriding of one attribute "widget". This ticket is about overriding of general attributes (for example label, required or something specific for given field) without total replacement of the field.

Added new version of patch which could be applied against trunk. And it is again with doctests - model_forms tests have not been ported to unittest yet.

comment:6 by Julien Phalip, 14 years ago

Severity: Normal
Type: New feature

comment:7 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:8 by Aymeric Augustin, 13 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:9 by Aymeric Augustin, 12 years ago

Status: reopenednew

comment:10 by Jacob, 12 years ago

Resolution: wontfix
Status: newclosed

There's a limit of what attributes can do; at some point you're going to have to override __init__. Widgets are common and annoying enough to have a special-case, but for everything else we should encourage people to override __init__ rather than adding hooks like this.

Note: See TracTickets for help on using tickets.
Back to Top