To see the raw SQL queries Django is running, do the following:
In your settings file (typically settings.py), ensure DEBUG is set to True
DEBUG = True
Then import connection from django.db and print connection.queries
import pprint
from django.db import connection
pprint.pprint(connection.queries)
If your queries are slowing you down, look into using select_related() to follow foreign keys
4 comments
see also debugsqlshell in django-debug-toolbar.
https://github.com/django-debug-toolbar/django-debug-toolbar/blob/master/debug_toolbar/management/commands/debugsqlshell.py
import logging l = logging.getLogger('django.db.backends') l.setLevel(logging.DEBUG) l.addHandler(logging.StreamHandler())
https://stackoverflow.com/questions/2314920/django-show-log-orm-sql-calls-from-python-shell>>> queryset = MyModel.objects.all() >>> print queryset.query SELECT "myapp_mymodel"."id", ... FROM "myapp_mymodel"
from django.db import connection print connection.queries
works for theupdate()
command as well.Leave a Reply