I have an admin for a model that has many items in list_display, including foreign key relationships and counts. I also have a couple of fields in list_filter.
I noticed that to display the list it made tons of queries, so I decided to optimize this by overriding get_queryset and adding select_related and prefetch_related. I managed to reduce hundreds of small queries into a single big query.
This is great for the 100 displayed models. However, it adds a ton of overhead for list_filter fields! list_filter works by going through all the objects in the table and collecting the different values. Since it is using the same queryset as the display, it is bringing all those relationships, doing all those joins, which are not really needed. So I realized that my great optimization makes for a worse performance
I searched the documentation to see if there is a different queryset for display and filter, but I haven’t found anything. Am I stumbling on a problem that has been solved or is there some room to improve here?
Have you explored using only() or defer() methods in your queryset to limit the fields fetched for list_filter while still optimizing the main display?