How can I check, which query django 1.4.11 generates for this query:
obj = Model.objects.get(code='code')
I've tried:
print Model.objects.get(code='code').query
but there is such method for model object. How can I get raw sql?
How can I check, which query django 1.4.11 generates for this query:
obj = Model.objects.get(code='code')
I've tried:
print Model.objects.get(code='code').query
but there is such method for model object. How can I get raw sql?
It doesn't work because query
is a property of the Queryset
object and when you do a .get()
the Queryset it´s evaluated (and became an instance of Model)
If you try:
>>> type(Model.objects.get(code='code'))
<class 'app.models.Model'>
>>> print Model.objects.get(code='code').query
AttributeError: 'Model' object has no attribute 'query'
But instead with:
>>> type(Model.objects.all())
<class 'django.db.models.query.QuerySet'>
>>> print Model.objects.all().query
SELECT "model.Model" from ...
Now, to get the SQL of all the queries you have several options:
If DEBUG=True
you can use this:
from django.db import connection
print connection.queries
Use django-debug-toolbar