Opened 7 years ago

Closed 6 years ago

Last modified 4 years ago

#8164 closed Uncategorized (fixed)

Custom field order in ModelForms

Reported by: kire Owned by: Alex
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 kire 7 years ago.
Patch proposal
fieldorderdocs.diff (1.4 KB) - added by kire 7 years ago.
Documentation
modelforms-fields-order.diff (2.9 KB) - added by Alex 7 years ago.

Download all attachments as: .zip

Change History (24)

Changed 7 years ago by kire

Patch proposal

comment:1 Changed 7 years ago by ericholscher

  • milestone set to post-1.0
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

comment:2 Changed 7 years ago by ericholscher

  • Needs documentation set
  • Needs tests set

Changed 7 years ago by kire

Documentation

comment:3 Changed 7 years ago by kire

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 7 years ago by kire

  • Needs documentation unset

comment:5 Changed 7 years ago by darkpixel

  • Cc django@… added

comment:6 Changed 7 years ago by aglzen

  • Cc steven@… added

comment:7 Changed 7 years ago by miracle2k

Changed 7 years ago by Alex

comment:8 Changed 7 years ago by Alex

  • Owner changed from nobody to Alex

comment:9 Changed 7 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:10 Changed 7 years ago by Alex

  • milestone set to 1.1
  • Needs tests unset
  • Triage Stage changed from Design decision needed to Accepted

#888 was a dupe.

comment:11 Changed 7 years ago by Alex

Err #8888

comment:12 Changed 6 years ago by anonymous

  • Cc hv@… added

comment:13 Changed 6 years ago by russellm

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

(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 6 years ago by mbi

  • 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 6 years ago by Alex

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

comment:16 Changed 6 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 follow-up: Changed 6 years ago by anentropic

  • Resolution fixed deleted
  • Status changed from closed to reopened

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

  • Resolution set to fixed
  • Status changed from reopened to closed

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

comment:20 Changed 4 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

comment:21 Changed 4 years ago by guettli

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