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)
Change History (13)
by , 14 years ago
Attachment: | formmodel_fields_attrs_r13353.patch added |
---|
by , 14 years ago
Attachment: | fields-kwargs.diff added |
---|
comment:1 by , 14 years ago
comment:2 by , 14 years ago
Owner: | changed from | to
---|
comment:3 by , 14 years ago
Needs documentation: | set |
---|---|
Patch needs improvement: | set |
Triage Stage: | Unreviewed → Design decision needed |
Tested 'fields-kwargs.diff', patch applied cleanly all tests ran. Test should be rewritten into unittests. Also needs documentation.
comment:4 by , 14 years ago
Cc: | removed |
---|---|
Needs documentation: | unset |
Patch needs improvement: | unset |
Resolution: | → fixed |
Status: | new → closed |
Actually this looks to have been implemented in 1.2:
http://docs.djangoproject.com/en/1.2/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets
by , 14 years ago
Attachment: | fields-kwargs.2.diff added |
---|
comment:5 by , 14 years ago
Resolution: | fixed |
---|---|
Status: | closed → 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 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → New feature |
comment:9 by , 12 years ago
Status: | reopened → new |
---|
comment:10 by , 12 years ago
Resolution: | → wontfix |
---|---|
Status: | new → 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.
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.