Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select_related() in generic class-based views

I'm just getting started with the new(ish) class-based views, and I am wondering what's the best way to get select_related() in there. Here's my view:

class PostDetailView(DetailView):
    model = Post

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

like image 207
Micah Carrick Avatar asked Sep 05 '11 18:09

Micah Carrick


1 Answers

Specify a queryset instead of model:

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

(See the docs).

like image 149
Ismail Badawi Avatar answered Oct 18 '22 02:10

Ismail Badawi