Opened 5 years ago

Closed 2 years ago

#13769 closed New feature (wontfix)

ModelForm: override default field attributes

Reported by: darkrho 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 darkrho 5 years ago.
fields-kwargs.diff (4.6 KB) - added by Petr Marhoun <petr.marhoun@…> 5 years ago.
fields-kwargs.2.diff (4.5 KB) - added by Petr Marhoun <petr.marhoun@…> 5 years ago.

Download all attachments as: .zip

Change History (13)

Changed 5 years ago by darkrho

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

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

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

  • Owner changed from nobody to asenchi

comment:3 Changed 5 years ago by asenchi

  • Needs documentation set
  • Patch needs improvement set
  • Triage Stage changed from Unreviewed to Design decision needed

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

comment:4 Changed 5 years ago by asenchi

  • Cc darkrho removed
  • Needs documentation unset
  • Patch needs improvement unset
  • Resolution set to fixed
  • Status changed from new to closed

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

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

  • Resolution fixed deleted
  • Status changed from closed to reopened

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 4 years ago by julien

  • Severity set to Normal
  • Type set to New feature

comment:7 Changed 4 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:8 Changed 4 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:9 Changed 2 years ago by aaugustin

  • Status changed from reopened to new

comment:10 Changed 2 years ago by jacob

  • Resolution set to wontfix
  • Status changed from new to closed

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