Select_related () in class-based generic views

I am just starting out with new (ish) classes and I am wondering what is the best way to get select_related () there. Here is my view:

class PostDetailView(DetailView): model = Post 

The message comes from "slug" in the url. This works fine, but I would like to get select_related () there to reduce the number of queries.

+6
source share
1 answer

Specify queryset instead of model :

 class PostDetailView(DetailView): queryset = Post.objects.select_related() 

(See documents ).

+16
source

Source: https://habr.com/ru/post/896657/


All Articles