You can print a queryset's SQL as follows:
print str(queryset.query)
however, for some reason this removes quotation marks, so you get:
SELECT `tableA`.`fieldA` FROM `fieldA` WHERE `tableA`.`fieldB` = Foo
instead of:
SELECT `tableA`.`fieldA` FROM `fieldA` WHERE `tableA`.`fieldB` = "Foo"
notice the missing ""
How can this be corrected?
not quite what you want, but if you have
DEBUG = True
you can useupdate:
looking at the
Queryset
__str__
method:If this is for debug purpose you should look into django-debug-toolbar that will show you all queries ran for any view you're looking at
If the underlying database is PostgreSQL you can do:
sql_with_params
returns the plain query without any values substituted and the parameters that will be inserted in the query.It is still not recommended to use
.mogrify()
for other purposes than debugging because the method may disappear in the future.If you want to execute the query, you can/should just use
.raw()
.