Opened 16 years ago

Closed 14 years ago

#10212 closed (duplicate)

Admin ChangeList doesn't apply 'order_by' clause specified by ModelAdmin.queryset

Reported by: kyle.fox@… Owned by: nobody
Component: contrib.admin Version: 1.0
Severity: Keywords: ChangeList, admin, ordering, queryset
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Overriding ModelAdmin.queryset to apply an explicit order_by clause doesn't work. Although it looks like it, the example shown won't order by '-birth_date'

class PersonAdmin(admin.ModelAdmin):
    list_display = ['name', 'birth_date']
    
    def queryset(self, request):
        return Person.objects.order_by('-birth_date')
        
admin.site.register(Person, PersonAdmin)

This patch changes ChangeList.get_ordering to respect any ordering clause that is present on the queryset.

Attachments (2)

10212.diff (1.1 KB ) - added by kyle.fox@… 16 years ago.
Patch for #10212 - Allows order_by in ModelAdmin.queryset
admin_ordering.diff (2.0 KB ) - added by mzzzzc 15 years ago.
More general patch. Also allows multi-level ordering and ordering given in query_set.extra().

Download all attachments as: .zip

Change History (7)

by kyle.fox@…, 16 years ago

Attachment: 10212.diff added

Patch for #10212 - Allows order_by in ModelAdmin.queryset

comment:1 by anonymous, 16 years ago

Component: Uncategorizeddjango.contrib.admin
Has patch: set

comment:2 by Jacob, 16 years ago

Triage Stage: UnreviewedDesign decision needed

Is there a reason not to just use ordering on the ModelAdmin?

comment:3 by kyle.fox@…, 16 years ago

Yes -- I'm using django-mptt, which has a custom manager to calculates the ordering of objects according to hierarchy.

Currently, if you try to use a custom manager which defines it's own ordering, the admin *drops* that order by clause.

comment:4 by oyvind, 16 years ago

I also hit this issue. Would be easier to solve with the patch in #9749.

by mzzzzc, 15 years ago

Attachment: admin_ordering.diff added

More general patch. Also allows multi-level ordering and ordering given in query_set.extra().

comment:5 by Julien Phalip, 14 years ago

Resolution: duplicate
Status: newclosed

This is a dupe of #7309.

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