Opened 12 years ago
Closed 12 years ago
#19514 closed Bug (wontfix)
form.fields.keyOrder is not supported for AdminForm
Reported by: | Andrew Scorpil | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | 1.4 |
Severity: | Normal | Keywords: | keyOrder, form, django-admin |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When you have redefined ModelAdmin and redefined ModelForm for change_view/add_view, this form will be modified to AdminForm. In process, django will automatically create fieldset, if it's not already there, by calling get_fieldset (django.contrib.admin.options.py:425):
def get_fieldsets(self, request, obj=None): "Hook for specifying fieldsets for the add form." if self.declared_fieldsets: return self.declared_fieldsets form = self.get_form(request, obj) fields = list(form.base_fields) + list(self.get_readonly_fields(request, obj)) return [(None, {'fields': fields})]
Obviously, this won't preserve any ordering that may have been specified in self.fields.keyOrder. I understand why this was implemented in such a way: keyOrder is specified (in most cases) when form is initialized, and AdminForm is created before that, but it would be great to have ordering functionality in place for django-admin, so i propose to think about the good way to fix this.
Steps to reproduce:
- Create simple model called, for example, CustomModel
- Create ModelForm based on this model. Let's call it CustomModelForm.
- In the CustomModelForm.__init__ specify something like self.fields.keyOrder = self.fields.keyOrder[::-1]
- Create CustomModelAdmin that inherits django.contrib.admin.ModelAdmin
- Set form = CustomModelForm under CustomModelAdmin
- Register CustomModel and CustomModelAdmin to the admin_site.
- Enter change_view or add_view from the browser. Order of fields stays as it is defined in model, while programmer expects it to be reversed.
The admin has a much more powerful way of specifying field order for the form, and it doesn't make sense to encourage a different way of specifying field order, when it is going to be overridden if the
ModelAdmin
definesfieldsets
. "Only one way to do it" seems to encourage a WONTFIX/INVALID here. Yes, we already have two potential ways to do it, but for the admin one has never worked and we've never advertised that it would work.