Skip to content

Instantly share code, notes, and snippets.

@iver56
Created October 23, 2015 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iver56/796ad44194278790eea5 to your computer and use it in GitHub Desktop.
Save iver56/796ad44194278790eea5 to your computer and use it in GitHub Desktop.
Dump the sql that django generates
import sqlparse
from django.db import connection
from django.core.signals import request_finished
def finished(sender, **kwargs):
for query in connection.queries:
print "\n" + "(" + query['time'] + " s) " + sqlparse.format(query['sql'], reindent=True, keyword_case="upper") + "\n"
if len(connection.queries) > 0:
print "NUMBER OF DB QUERIES FOR THIS REQUEST:", len(connection.queries)
request_finished.connect(finished)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment