eugene
eugene

Reputation: 41665

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?

Upvotes: 10

Views: 3489

Answers (1)

solarissmoke
solarissmoke

Reputation: 31404

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.

Upvotes: 11

Related Questions