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.
我想看看在django的模型.save()方法上执行了什么查询。由于我在生产环境中,我不能使用Django Toolbar。
2 个解决方案
#1
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/
在Django 1.3及更高版本中,您可以使用我认为将您的SQL查询转储到日志中的日志记录。 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
如果没有DEBUG = True,似乎没有直接的简单方法。这是我能找到的最接近的:将DEBUG设置为False的Django SQL查询记录
#2
4
Based on Sid's answer and this snippet (http://djangosnippets.org/snippets/1973/) i've replace the postgres db-wrapper with this:
基于Sid的答案和这个片段(http://djangosnippets.org/snippets/1973/)我用这个替换了postgres db-wrapper:
# 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'
,)
然后在settings.py中,使用'ENGINE':'my_project.db_backend',而不是默认的后端(在我的例子中,'ENGINE':'django.db.backends.postgresql_psycopg2',)
Now connection.queries
will contain all your queries!
现在connection.queries将包含您的所有查询!
#1
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/
在Django 1.3及更高版本中,您可以使用我认为将您的SQL查询转储到日志中的日志记录。 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
如果没有DEBUG = True,似乎没有直接的简单方法。这是我能找到的最接近的:将DEBUG设置为False的Django SQL查询记录
#2
4
Based on Sid's answer and this snippet (http://djangosnippets.org/snippets/1973/) i've replace the postgres db-wrapper with this:
基于Sid的答案和这个片段(http://djangosnippets.org/snippets/1973/)我用这个替换了postgres db-wrapper:
# 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'
,)
然后在settings.py中,使用'ENGINE':'my_project.db_backend',而不是默认的后端(在我的例子中,'ENGINE':'django.db.backends.postgresql_psycopg2',)
Now connection.queries
will contain all your queries!
现在connection.queries将包含您的所有查询!