#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)
Change History (24)
Changed 15 years ago by
Attachment: | fieldsort.diff added |
---|
comment:1 Changed 15 years ago by
milestone: | → post-1.0 |
---|---|
Triage Stage: | Unreviewed → Design decision needed |
comment:2 Changed 15 years ago by
Needs documentation: | set |
---|---|
Needs tests: | set |
comment:3 Changed 15 years ago by
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 15 years ago by
Needs documentation: | unset |
---|
comment:5 Changed 15 years ago by
Cc: | django@… added |
---|
comment:6 Changed 15 years ago by
Cc: | steven@… added |
---|
comment:7 Changed 15 years ago by
Apparently related ticket:
http://code.djangoproject.com/ticket/5986
Changed 15 years ago by
Attachment: | modelforms-fields-order.diff added |
---|
comment:8 Changed 15 years ago by
Owner: | changed from nobody to Alex Gaynor |
---|
comment:10 Changed 15 years ago by
milestone: | → 1.1 |
---|---|
Needs tests: | unset |
Triage Stage: | Design decision needed → Accepted |
#888 was a dupe.
comment:12 Changed 15 years ago by
Cc: | hv@… added |
---|
comment:13 Changed 15 years ago by
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:14 Changed 15 years ago by
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 15 years ago by
If you think there's a bug in the code that went in, please file a bug.
comment:16 Changed 15 years ago by
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: 18 Changed 14 years ago by
Resolution: | fixed |
---|---|
Status: | closed → 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 Changed 14 years ago by
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 14 years ago by
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
If you want to request a new feature, file a new ticket.
comment:21 Changed 12 years ago by
Cc: | hv@… removed |
---|---|
Easy pickings: | unset |
Severity: | → Normal |
Type: | → Uncategorized |
UI/UX: | unset |
Patch proposal