Opened 6 years ago

Closed 4 years ago

#13769 closed New feature (wontfix)

ModelForm: override default field attributes

Reported by: Rolando Espinoza La fuente Owned by: asenchi
Component: Forms Version: master
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 6 years ago.
fields-kwargs.diff (4.6 KB) - added by Petr Marhoun <petr.marhoun@…> 6 years ago.
fields-kwargs.2.diff (4.5 KB) - added by Petr Marhoun <petr.marhoun@…> 6 years ago.

Download all attachments as: .zip

Change History (13)

Changed 6 years ago by Rolando Espinoza La fuente

Changed 6 years ago by Petr Marhoun <petr.marhoun@…>

Attachment: fields-kwargs.diff added

comment:1 Changed 6 years ago by Petr Marhoun <petr.marhoun@…>

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 Changed 6 years ago by asenchi

Owner: changed from nobody to asenchi

comment:3 Changed 6 years ago by asenchi

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 Changed 6 years ago by asenchi

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

Changed 6 years ago by Petr Marhoun <petr.marhoun@…>

Attachment: fields-kwargs.2.diff added

comment:5 Changed 6 years ago by Petr Marhoun <petr.marhoun@…>

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 Changed 6 years ago by Julien Phalip

Severity: Normal
Type: New feature

comment:7 Changed 5 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:8 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:9 Changed 4 years ago by Aymeric Augustin

Status: reopenednew

comment:10 Changed 4 years ago by Jacob

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