Opened 16 years ago

Closed 15 years ago

Last modified 13 years ago

#8164 closed Uncategorized (fixed)

Custom field order in ModelForms

Reported by: killiands Owned by: Alex Gaynor
Component: Forms Version: dev
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 16 years ago.
Patch proposal
fieldorderdocs.diff (1.4 KB ) - added by killiands 16 years ago.
Documentation
modelforms-fields-order.diff (2.9 KB ) - added by Alex Gaynor 16 years ago.

Download all attachments as: .zip

Change History (24)

by killiands, 16 years ago

Attachment: fieldsort.diff added

Patch proposal

comment:1 by Eric Holscher, 16 years ago

milestone: post-1.0
Triage Stage: UnreviewedDesign decision needed

comment:2 by Eric Holscher, 16 years ago

Needs documentation: set
Needs tests: set

by killiands, 16 years ago

Attachment: fieldorderdocs.diff added

Documentation

comment:3 by killiands, 16 years ago

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 by killiands, 16 years ago

Needs documentation: unset

comment:5 by Aaron C. de Bruyn, 16 years ago

Cc: django@… added

comment:6 by Steven Skoczen, 16 years ago

Cc: steven@… added

comment:7 by miracle2k, 16 years ago

by Alex Gaynor, 16 years ago

comment:8 by Alex Gaynor, 16 years ago

Owner: changed from nobody to Alex Gaynor

comment:9 by (none), 16 years ago

milestone: post-1.0

Milestone post-1.0 deleted

comment:10 by Alex Gaynor, 16 years ago

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

#888 was a dupe.

comment:11 by Alex Gaynor, 16 years ago

Err #8888

comment:12 by anonymous, 16 years ago

Cc: hv@… added

comment:13 by Russell Keith-Magee, 16 years ago

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 by Marco Bonetti, 16 years ago

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 by Alex Gaynor, 16 years ago

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

comment:16 by chrisrbennett, 16 years ago

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 by Paul Garner, 15 years ago

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

in reply to:  17 comment:18 by Paul Garner, 15 years ago

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 by Russell Keith-Magee, 15 years ago

Resolution: fixed
Status: reopenedclosed

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

comment:20 by Jacob, 13 years ago

milestone: 1.1

Milestone 1.1 deleted

comment:21 by Thomas Güttler, 13 years ago

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