Django — Override ModelAdmin ChangeList Ordering By Annotate and Aggregate


I ran into trouble overriding django’s ModelAdmin ChangeList Ordering.

Overriding the ModelAdmin.queryset did not help because while the Queryset can be defined, the ordering is overridden by `ChangeList.get_ordering`

My solution is to override the ModelAdmin get_changelist method and return a subclassed ChangeList.

class AccountAdmin(admin.ModelAdmin):   
    # ...
    def get_changelist(self, request, **kwargs):
        Returns the ChangeList class for use on the changelist page.
        Force get_ordering to return None (if no ordering specified)
        to prevent  from applying ordering.
        from django.contrib.admin.views.main import ChangeList
        class SortedChangeList(ChangeList):
            def get_query_set(self, *args, **kwargs):
                qs = super(SortedChangeList, self).get_query_set(*args, **kwargs)
                return qs.annotate(amt=Sum('entry__amount')).order_by('-amt')
        if request.GET.get('o'):
            return ChangeList
        return SortedChangeList

We take advantage of the fact that get_changelist can be overridden, and the fact that get_query_set is called AFTER ordering is done on the ChangeList.

Let me know if you have other solutions!


8 thoughts on “Django — Override ModelAdmin ChangeList Ordering By Annotate and Aggregate

    1. So as to not break default admin ordering. Otherwise the only ordering we could apply is our custom one.

      Also, Django 1.4 has multi column ordering and the syntax has changed so we’ll have to account for that in the future…

  1. ah, cool, thanks. Yes, I saw in the docs for 1.4 that’t it can be done a little more elegantly there – but that’ll have to wait till 1.4 is the recommended stable version.

  2. Derek, I have just implemented with 1.4
    I’m using it a bit differently as I’m overriding the search query to allow users to not have to enter in dashes when searching for phone number

    def get_changelist(self, request, **kwargs):
    # Allow users to not have to enter in ‘-‘ when searching by phone #
    from django.contrib.admin.views.main import ChangeList

    if request.GET.get(‘q’):
    class NewChangeList(ChangeList):
    def get_query_set(self, *args, **kwargs):
    query = self.query
    if query.isdigit() and len(query) == 10:
    self.query = ‘-‘.join(
    (query[:3], query[3:6], query[6:])
    return super(NewChangeList, self).get_query_set(
    *args, **kwargs
    return NewChangeList
    return ChangeList

  3. OK; that is interesting. I have been struggling to get a search working on date or datetime fields…

  4. Maybe you can add that new code to your original post as an update; the formatting gets lost in the comments…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s