Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display query generated by prefetch_related

I have a QuerySet with a prefetch_related() (with Prefetch object)

I want to see the raw query and print(qs.query) and it doesn't show anything about the prefetch_related stuff.

How do I see the query which will be run because of prefetch_related?

like image 804
eugene Avatar asked Oct 18 '15 10:10

eugene


1 Answers

The queryset's query object will only show you the query that Django generates for the queryset itself (before any prefetch_related etc. is applied).

You probably need to look at these guidelines for inspecting the query that is actually sent to your database:

from django.db import connection
print(connection.queries)

Alternatively you can use something like django-debug-toolbar to display the queries.

like image 146
solarissmoke Avatar answered Nov 15 '22 04:11

solarissmoke