I want to see what queries are executed on django's model .save() method. Since I am in a production environment, I can't use Django Toolbar for this.
Asked
Active
Viewed 7,219 times
2 Answers
5
Based on Sid's answer and this snippet (http://djangosnippets.org/snippets/1973/) i've replace the postgres db-wrapper with this:
# base.py
from django.db.backends.postgresql_psycopg2.base import *
#http://djangosnippets.org/snippets/1973/
class DatabaseWrapper(DatabaseWrapper):
def __init__(self, *args, **kwargs):
super(DatabaseWrapper, self).__init__(*args, **kwargs)
self.use_debug_cursor = True
Then in settings.py
, use 'ENGINE': 'my_project.db_backend'
, instead of the default backend (in my case, 'ENGINE': 'django.db.backends.postgresql_psycopg2'
,)
Now connection.queries
will contain all your queries!

StefanNch
- 2,569
- 24
- 31
2
There are 2 ways:
In Django 1.3 and above you can use logging which I believe dumps your sql queries into the log. https://docs.djangoproject.com/en/dev/topics/logging/
Doesn't seem like there's a straight-forward easy way without DEBUG=True. This is the closest I could find: Logging Django SQL queries with DEBUG set to False
-
right, but the question asks specifically for .save() and DEBUG=True is not an option since I can't set my live server in DEBUG. I also can't reproduce my issue on my debug environment. – Mihai Oprea Mar 28 '12 at 16:04
-
Doesn't seem like there's a straight-forward easy way without DEBUG=True. This is the closest I could find: http://stackoverflow.com/questions/7181511/logging-django-sql-queries-with-debug-set-to-false – Sid Mar 28 '12 at 16:14
-
Maybe you can set your database to [log queries](http://www.postgresql.org/docs/8.3/static/runtime-config-logging.html#GUC-LOG-STATEMENT) (beware: slows it up) – RickyA Jan 16 '13 at 15:02