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