So for a long time I've been doing annotations in ModelAdmins to add complicated number and related fields to list admin displays. Here's a simple count example.
class MyClassAdmin(admin.ModelAdmin):
list_display [ .. 'capacity', ..]
def get_queryset(self, request):
return super(MyClassAdmin, self).get_queryset(request).annotate(
capacity=Count('spaces')
)
def capacity(self, obj):
return obj.capacity
In practice I'm doing four or five on some ModelAdmins. The query is slower than the stock query, but it's faster than doing 1+100 queries as the related data is pulled in.
However, Django does a count query on list views. Under the table you'll see a xxx modelnames
message. This is generated from the same, annotated query. Verified with Django Debug Toolbar. I find that in some cases, any saving from reducing the number of queries is lost to the more complicated count query.
I know overriding get_queryset
allows you to filter down the data but that's not how I'm using it. Is there something better I can override which allows the count to go through unannotated, but lets me annotate for the list display?