2

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?

Paul
  • 6,641
  • 8
  • 41
  • 56

1 Answers1

2

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:

Community
  • 1
  • 1
fasouto
  • 4,386
  • 3
  • 30
  • 66