November 2015
Beginner to intermediate
840 pages
26h 30m
English
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
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() ...
Read now
Unlock full access