How do I get the SQL that Django will use on the database from a QuerySet object? I'm trying to debug some strange behavior, but I'm not sure what queries are going to the database.
-
10This isn't a duplicate. the linked question is quite a different topic.– craigdsMar 23, 2017 at 20:45
-
2@Wooble this isn't a duplicate. It's in the same area as the other question but this question relates to a specific query, not ALL queries.– AkrikosAug 22, 2019 at 18:08
5 Answers
You print the queryset's query
attribute.
>>> queryset = MyModel.objects.all()
>>> print(queryset.query)
SELECT "myapp_mymodel"."id", ... FROM "myapp_mymodel"
-
I found this which mentions it implicitly but nothing that explicitly documents the above Sep 24, 2013 at 22:41
-
48Note that the output of
query
is not valid SQL, because "Django never actually interpolates the parameters: it sends the query and the parameters separately to the database adapter, which performs the appropriate operations." Source: code.djangoproject.com/ticket/17741 Jul 7, 2014 at 14:50 -
2Is it possible to see the SQL query before the it is actually executed? I'm getting a database error and want to see what SQL it's trying to execute, but when I run this I simply get a database error and cannot see the full SQL.– ArielJan 5, 2021 at 12:43
-
1If you do anything other than
print
, you may have to usestr(queryset.query)
, becausetype(queryset.query) == Query
. Aug 4, 2021 at 14:35 -
4To get the valid SQL from Postgres, use this: stackoverflow.com/questions/8112554/…– fjsjNov 24, 2021 at 15:00
Easy:
print(my_queryset.query)
For example:
from django.contrib.auth.models import User
print(User.objects.filter(last_name__icontains = 'ax').query)
It should also be mentioned that if you have DEBUG = True, then all of your queries are logged, and you can get them by accessing connection.queries:
from django.db import connections
connections['default'].queries
The django debug toolbar project uses this to present the queries on a page in a neat manner.
-
3
-
2There is no way to get actual SQL without executing the query first, final SQL is generated by the surrounding RDBMS driver, not Django. The answer is correct as it's the most you can get with Django QuerySet.– daniusJan 10, 2016 at 7:48
The accepted answer did not work for me when using Django 1.4.4. Instead of the raw query, a reference to the Query object was returned: <django.db.models.sql.query.Query object at 0x10a4acd90>
.
The following returned the query:
>>> queryset = MyModel.objects.all()
>>> queryset.query.__str__()
-
33It probably didn't work because you just typed
queryset.query
instead ofprint queryset.query
, which calls__str__()
– hughesMay 28, 2013 at 17:49 -
16@hughes is right. If you don't want to print it and want it as a string, instead of calling
__str__()
to get it as a string you should dostr(queryset.query)
.– ChadMay 30, 2013 at 23:31 -
1I am using ipdb to debug and it prints a reference to the query object when I do
p queryset
.p queryset.__str__()
produces the desired result so this is a better answer.– RafayJun 25, 2014 at 5:15 -
21
-
This middleware will output every SQL query to your console, with color highlighting and execution time, it's been invaluable for me in optimizing some tricky requests
As an alternative to the other answers, django-devserver outputs SQL to the console.