[Fixed]-Django Select Related in Template Paginated Search Results to Reduce Queries?

1👍

select_related only works for foreign keys. For many to many fields you can use prefetch_related. I think that with prefect related you have to specify the related models you want to fetch, and that you can’t call it with no arguments like select_related.

Note that you shouldn’t need 25 queries for 25 products. It should be possible to reduce it to one query for the products queryset, one additional query for each prefetch_related argument, and (because you are using a paginator) one for the total number of objects.

Leave a comment