exupero exupero - 1 year ago 184
SQL Question

Getting the SQL from a Django QuerySet

How do I get the SQL that Django will use on the database from a QuerySet object? I'm trying to debug some strange behavior, but I'm not sure what queries are going to the database. Thanks for your help.

Answer Source

You print the queryset's query attribute.

>>> queryset = MyModel.objects.all()
>>> print queryset.query
SELECT "myapp_mymodel"."id", ... FROM "myapp_mymodel"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download