26.3.9 Optimizing Admin Pages

Our views and templates are not the only places we should consider optimization. In the admin, our list of User objects lists the name and joined fields on the related Profile object, resulting in a database query for each User in the list at the moment. We can reduce all of these queries to a single one thanks to the list_select_related, which will use select_related() with any parameters we pass to the tuple, as shown in Example 26.38.

Example 26.38: Project Code

user/admin.py in 3e60afbdbe

39   class UserAdmin(admin.ModelAdmin):  .       ... 53       list_select_related = ('profile',)

If we wanted to be able to use prefetch_related(), we would have to do so in the get_queryset() ...

Get Django Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.