Django: show/log ORM sql calls from python shell Django: show/log ORM sql calls from python shell django django

Django: show/log ORM sql calls from python shell


If you're using Django 1.3:

import loggingl = logging.getLogger('django.db.backends')l.setLevel(logging.DEBUG)l.addHandler(logging.StreamHandler())


I was trying to use "Django: show/log ORM sql calls from python shell" in a shell on a production server, and it wasn't working. Eventually someone pointed out that it will only do this debug logging when DEBUG = True. But you can work around that like this:

import loggingfrom django.db import connectionconnection.force_debug_cursor = True  # Change to use_debug_cursor in django < 1.8l = logging.getLogger('django.db.backends')l.setLevel(logging.DEBUG)l.addHandler(logging.StreamHandler())

I'm leaving this here so I can find it later, and hopefully it saves someone else the same digging I did.


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.