tags:

views:

202

answers:

2

Using the excellent Django-Devserver I'm finding all kinds of interesting and unexpected SQL calls in my code. I wanted to find where the calls are coming from, and so I'm looking for a way to get a log or print-out of all SQL calls generated by the Django ORM in the Python shell. That is, when I do a Django ORM call via the Python shell, I'd like to see the resulting SQL printed out or logged.

I noticed several solutions that add log info to the html page. Is there an easy way to dump to the command line instead?

+1  A: 

Rob Hudson's Django Debug Toolbar, as well as its general awesomness, also includes a pretty nifty debugsqlshell manage.py command which does exactly this.

Daniel Roseman
+3  A: 

If you're in the shell, or anywhere for that matter, you can use the queryset method

query.as_sql()

to print the SQL command.

ie:

MyModel.objects.all().query.as_sql()
Zach