Sunday, February 01, 2009

Djanog show model query

Show the raw SQL generated by a model access: 
# DEBUG needs to be set True 
>>> from django.db import connection 
>>> connection.queries [{'sql': 'SELECT polls_polls.id,polls_polls.question,polls_polls.pub_date FROM polls_polls', 'time': '0.002'}]

sql # The raw SQL statement 
time # How long the statement took to execute, in seconds.

Reset the queries list returned above:
>>> from django import db
>>> db.reset_queries()

Show SQL for a given query (newer django version; 1.5+?)
>>> Product.objects.all().query.sql_with_params()

Show SQL for a given query (older django version; 1.2-1.5+?)
>>> Product.objects.all().query.as_sql()




No comments: