3. How to enable sorting on calculated fields?

Django adds sorting capabilities on fields which are attributes on the models. When you add a calculated field Django doesn’t know how to do a order_by, so it doesn’t add sorting capability on that field.

If you want to add sorting on a calculated field, you have to tell Django what to pass to order_by. You can do this by setting the admin_order_field attribute on the calculated field method.

You start from the admin you wrote in the previous chapter (How to optimize queries in Django admin?).:

hero_count.admin_order_field = '_hero_count'
villain_count.admin_order_field = '_villain_count'

With these changes your admin becomes:

@admin.register(Origin)
class OriginAdmin(admin.ModelAdmin):
    list_display = ("name", "hero_count", "villain_count")

    def get_queryset(self, request):
        queryset = super().get_queryset(request)
        queryset = queryset.annotate(
            _hero_count=Count("hero", distinct=True),
            _villain_count=Count("villain", distinct=True),
        )
        return queryset

    def hero_count(self, obj):
        return obj._hero_count

    def villain_count(self, obj):
        return obj._villain_count

    hero_count.admin_order_field = '_hero_count'
    villain_count.admin_order_field = '_villain_count'

Here is the admin sorted on hero_count

_images/sorting_calculated_field.png