Opened 8 years ago

Closed 7 years ago

Last modified 5 years ago

#8164 closed Uncategorized (fixed)

Custom field order in ModelForms

Reported by: killiands Owned by: Alex Gaynor
Component: Forms Version: master
Severity: Normal Keywords: modelform, field order
Cc: django@…, steven@…, marco@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When deriving forms from models it may be wanted to have a custom ordering and intertwine extra fields with the predefined model fields. Accompanied patch allows this:

Example given:

class UserForm(forms.ModelForm):
    password = forms.CharField(label=_('password'), widget=forms.PasswordInput)
    password2 = forms.CharField(label=_('repeat password'), widget=forms.PasswordInput)
    email2 = forms.EmailField(label=_('repeat email'))
    
    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'password', 'password2', 'email', 'email2')

As you can see you can both define model fields and custom fields in the meta class and now the form will use that order instead of the model order followed by custom fields.

Attachments (3)

fieldsort.diff (870 bytes) - added by killiands 8 years ago.
Patch proposal
fieldorderdocs.diff (1.4 KB) - added by killiands 8 years ago.
Documentation
modelforms-fields-order.diff (2.9 KB) - added by Alex Gaynor 8 years ago.

Download all attachments as: .zip

Change History (24)

Changed 8 years ago by killiands

Attachment: fieldsort.diff added

Patch proposal

comment:1 Changed 8 years ago by Eric Holscher

milestone: post-1.0
Triage Stage: UnreviewedDesign decision needed

comment:2 Changed 8 years ago by Eric Holscher

Needs documentation: set
Needs tests: set

Changed 8 years ago by killiands

Attachment: fieldorderdocs.diff added

Documentation

comment:3 Changed 8 years ago by killiands

I added this ticket because I think it's a minor patch that would make things much easier when creating customized model forms. Now you have to 'hack' the output, just to change field order. Too bad it's set at post-1.0. It actually doesn't break anything (tested it on new fields, model fields, overridden fields & non-existant fields).

comment:4 Changed 8 years ago by killiands

Needs documentation: unset

comment:5 Changed 8 years ago by Aaron C. de Bruyn

Cc: django@… added

comment:6 Changed 8 years ago by Steven Skoczen

Cc: steven@… added

comment:7 Changed 8 years ago by miracle2k

Changed 8 years ago by Alex Gaynor

comment:8 Changed 8 years ago by Alex Gaynor

Owner: changed from nobody to Alex Gaynor

comment:9 Changed 8 years ago by (none)

milestone: post-1.0

Milestone post-1.0 deleted

comment:10 Changed 8 years ago by Alex Gaynor

milestone: 1.1
Needs tests: unset
Triage Stage: Design decision neededAccepted

#888 was a dupe.

comment:11 Changed 8 years ago by Alex Gaynor

Err #8888

comment:12 Changed 8 years ago by anonymous

Cc: hv@… added

comment:13 Changed 8 years ago by Russell Keith-Magee

Resolution: fixed
Status: newclosed

(In [10062]) Fixed #8164 -- Fields on a ModelForm are now ordered in the order specified in the fields attribute of the ModelForm's Meta class. Thanks to Alex Gaynor for the patch.

comment:14 Changed 8 years ago by Marco Bonetti

Cc: marco@… added

Is it just me or doesn't the checked-in fix work as in the original example? I get a nasty error when I try to create a ModelForm having extra fields in the 'fields' list that aren't in the Model.

comment:15 Changed 8 years ago by Alex Gaynor

If you think there's a bug in the code that went in, please file a bug.

comment:16 Changed 8 years ago by chrisrbennett

The added iteration of Meta fields in this patch will break (faulty) assignment of field names not properly sequenced.

Previously, fields = ("foofield") worked. Now it doesn't. I'm sure I'm not the only person that falls into the "want one item dict, but forgot trailing comma" trap.

comment:17 Changed 7 years ago by anentropic

Resolution: fixed
Status: closedreopened

This fix only seems to apply to ModelForms... surely we should be able to do the same thing for regular forms too? If you want one form to inherit off another?

Here's a hack I found on StackOverflow to achieve the neccessary order with a regular form:
http://stackoverflow.com/questions/350799/how-does-django-know-the-order-to-render-form-fields/1191310#1191310

comment:18 in reply to:  17 Changed 7 years ago by anentropic

Replying to anentropic:

This fix only seems to apply to ModelForms... surely we should be able to do the same thing for regular forms too? If you want one form to inherit off another?

Here's a hack I found on StackOverflow to achieve the neccessary order with a regular form:
http://stackoverflow.com/questions/350799/how-does-django-know-the-order-to-render-form-fields/1191310#1191310

(example they use is a ModelForm but I tried it and it works with regular Form)

comment:19 Changed 7 years ago by Russell Keith-Magee

Resolution: fixed
Status: reopenedclosed

If you want to request a new feature, file a new ticket.

comment:20 Changed 5 years ago by Jacob

milestone: 1.1

Milestone 1.1 deleted

comment:21 Changed 5 years ago by Thomas Güttler

Cc: hv@… removed
Easy pickings: unset
Severity: Normal
Type: Uncategorized
UI/UX: unset
Note: See TracTickets for help on using tickets.
Back to Top