26.3.7 Optimizing Views with Related Content
We’ve seen select_related()
, prefetch_related()
, Q
and Prefetch
objects, and we know how to profile with queryset.query
and connection
. It’s time to put all our tools to work.
In our generic class-based views (GCBVs), we have used the model
attribute to tell our views which model to fetch. Most GCBVs also allow for the use of the queryset
, which enables us to define the queryset used on the model
(which now doesn’t need to be set). We can, for instance, use the select_related()
method in PostDetail
to fetch both the Post
object and the User
object related by the author
field, as shown in Example 26.31.
48 class ...
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.