Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django getting executable raw sql for a QuerySet

I know that you can get the SQL of a given QuerySet using

print query.query

but as we know from a previous question ( Potential Django Bug In QuerySet.query? ) the returned SQL is not properly quoted. See http://code.djangoproject.com/browser/django/trunk/django/db/models/sql/query.py

Is there any way that is it possible to get the raw, executable SQL (quoted) for a given QuerySet without actually executing it?

like image 976
epoch Avatar asked Sep 22 '10 12:09

epoch


1 Answers

Django never creates the raw sql, so no. To prevent SQL injection, django passes the parameters separately to the database drivers at the last step. The best way to get the actual SQL is to look at your query log, which you cannot do before you execute the query.

like image 66
Mike Axiak Avatar answered Sep 30 '22 18:09

Mike Axiak