Django

Code

Ticket #8164 (closed: fixed)

Opened 2 years ago

Last modified 3 weeks ago

Custom field order in ModelForms

Reported by: kire Assigned to: Alex
Milestone: 1.1 Component: Forms
Version: SVN Keywords: modelform, field order
Cc: django@darkpixel.com, steven@quantumimagery.com, hv@tbz-pariv.de, marco@electronlibre.ch Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

fieldsort.diff (0.8 kB) - added by kire on 08/08/08 13:12:25.
Patch proposal
fieldorderdocs.diff (1.4 kB) - added by kire on 08/08/08 15:04:15.
Documentation
modelforms-fields-order.diff (2.9 kB) - added by Alex on 02/22/09 14:09:30.

Change History

08/08/08 13:12:25 changed by kire

  • attachment fieldsort.diff added.

Patch proposal

08/08/08 14:31:40 changed by ericholscher

  • needs_better_patch changed.
  • needs_docs changed.
  • stage changed from Unreviewed to Design decision needed.
  • needs_tests changed.
  • milestone set to post-1.0.

08/08/08 14:31:50 changed by ericholscher

  • needs_docs set to 1.
  • needs_tests set to 1.

08/08/08 15:04:15 changed by kire

  • attachment fieldorderdocs.diff added.

Documentation

08/08/08 15:09:49 changed 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).

08/08/08 15:51:42 changed by kire

  • needs_docs deleted.

08/17/08 18:09:14 changed by darkpixel

  • cc set to django@darkpixel.com.

10/05/08 15:51:44 changed by aglzen

  • cc changed from django@darkpixel.com to django@darkpixel.com, steven@quantumimagery.com.

02/12/09 22:17:46 changed by miracle2k

Apparently related ticket: http://code.djangoproject.com/ticket/5986

02/22/09 14:09:30 changed by Alex

  • attachment modelforms-fields-order.diff added.

02/24/09 19:52:47 changed by Alex

  • owner changed from nobody to Alex.

02/25/09 13:51:44 changed by

  • milestone deleted.

Milestone post-1.0 deleted

02/25/09 16:26:30 changed by Alex

  • stage changed from Design decision needed to Accepted.
  • needs_tests deleted.
  • milestone set to 1.1.

#888 was a dupe.

02/25/09 16:26:48 changed by Alex

Err #8888

03/09/09 10:18:27 changed by anonymous

  • cc changed from django@darkpixel.com, steven@quantumimagery.com to django@darkpixel.com, steven@quantumimagery.com, hv@tbz-pariv.de.

03/15/09 00:05:27 changed by russellm

  • status changed from new to closed.
  • resolution set to fixed.

(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.

03/16/09 07:06:22 changed by mbi

  • cc changed from django@darkpixel.com, steven@quantumimagery.com, hv@tbz-pariv.de to django@darkpixel.com, steven@quantumimagery.com, hv@tbz-pariv.de, marco@electronlibre.ch.

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.

03/16/09 08:08:04 changed by Alex

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

03/20/09 08:06:06 changed 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.

(follow-up: ↓ 18 ) 01/21/10 05:19:03 changed by anentropic

  • status changed from closed to reopened.
  • resolution deleted.

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 ) 01/21/10 05:20:16 changed 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)

01/21/10 05:54:10 changed by russellm

  • status changed from reopened to closed.
  • resolution set to fixed.

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


Add/Change #8164 (Custom field order in ModelForms)




Change Properties
Action