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:

  1. Create simple model called, for example, CustomModel
  2. Create ModelForm based on this model. Let's call it CustomModelForm.
  3. In the CustomModelForm.__init__ specify something like self.fields.keyOrder = self.fields.keyOrder[::-1]
  4. Create CustomModelAdmin that inherits django.contrib.admin.ModelAdmin
  5. Set form = CustomModelForm under CustomModelAdmin
  6. Register CustomModel and CustomModelAdmin to the admin_site.
  7. 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.

Change History (1)

comment:1 by Luke Plant, 12 years ago

Resolution: wontfix
Status: newclosed

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 defines fieldsets. "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.

Note: See TracTickets for help on using tickets.
Back to Top