Opened 17 years ago
Last modified 17 years ago
#6369 closed
ModelForm fields ordering when Meta.fields is set — at Version 1
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | yes |
Needs tests: | yes | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Field ordering is currently determined by the order the fields were originally defined in the model. However, if the user specifically defines the fields to use in Meta.fields, it's safe to assume that's the order the user wants the fields to be outputted in.
Right now, this is accomplished with:
class myForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(myForm, self).__init__(*args, **kwargs) self.fields.keyOrder = self.Meta.fields #This line should be unnecessary class Meta: model = myModel fields = ['field_3', 'field_2', 'field_9']
The new version would just be:
class myForm(forms.ModelForm): class Meta: model = myModel fields = ['field_3', 'field_2', 'field_9']
Change History (2)
by , 17 years ago
Attachment: | modelfield_order.diff added |
---|
comment:1 by , 17 years ago
Description: | modified (diff) |
---|---|
Needs documentation: | set |
Needs tests: | set |
Patch needs improvement: | set |
Triage Stage: | Unreviewed → Accepted |
Note:
See TracTickets
for help on using tickets.
Sounds like the right thing to do. It's even a TODO in the code comments :)
As far as the patch goes, assigning directly to
keyOrder
is not a good idea. IMO,keyOrder
looks like it should be a private attribute. Anyway, by blindly assigning tokeyOrder
without checking to see if those keys exist in theSortedDict
would cause bad things to happen.