如何在 Django 中记录所有 sql 查询?
如何记录 django 应用程序执行的所有 SQL 查询?
我想记录所有内容,包括来自管理站点的 SQL。我看到这个问题和常见问题解答 但我仍然不知道我应该把
from django.db import connection
connection.queries
所有内容记录到一个文件中?
所以我的问题是 - 我应该怎么做才能拥有一个记录所有 SQL 语句的文件(例如 all-sql.log)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
将以下代码段与
settings.py
中的LOGGING
字段合并:从 @acardenas89 答案调整
Merge the following snippet with the
LOGGING
field in yoursettings.py
:Tweaked from @acardenas89 answer
在settings.py中添加以下粗体语句
Resource/Credit
Add the following bold statements in settings.py
Resource/Credit
要在测试期间记录 SQL 查询,您需要两件事:
django.db.backends
记录器和@override_settings(DEBUG=True)
装饰器。测试运行器将 设置 DEBUG=False 默认情况下,忽略您在 DJANGO_SETTINGS_MODULE 中设置的内容。
最低设置:
示例测试用例:
To log SQL queries during testing, you need two things:
django.db.backends
logger enabled and@override_settings(DEBUG=True)
decorator.Test runner will set DEBUG=False by default, ignoring what you may have set in DJANGO_SETTINGS_MODULE.
The minimum settings:
The example test case:
也许查看 https://github.com/django-debug-toolbar/django- debug-toolbar
它可以让您查看给定页面生成的所有查询。以及它们发生位置的堆栈跟踪等。
编辑:将所有 SQL 查询记录到文件等,那么您将需要创建一些中间件。中间件根据每个请求运行。对于此类事情,有几个 Django 片段:
这些与打印到终端,但让它们适应使用 python 的日志库并不困难。
Maybe check out https://github.com/django-debug-toolbar/django-debug-toolbar
It'll let you see all the queries generated by a given page. As well as stacktraces of where they occur etc.
EDIT: to log all SQL queries to a file etc, then you will want to create some middleware. Middleware gets run on every request. There are several Django snippets out there for this sort of thing:
Those are concerned with printing to the terminal, but it wouldn't be hard to adapt them to use python's logging library.
Django 1.3 将所有 SQL 语句记录到 django.db.backends 记录器:
https://docs.djangoproject.com/en/dev/ref/logging/#django-db-backends
Django 1.3 logs all SQL statements to django.db.backends logger:
https://docs.djangoproject.com/en/dev/ref/logging/#django-db-backends
您只需要:
如果您已经在
runserver
中打印了 SQL 调试语句。将装饰器添加到您的
class TestA(TestCase)
或test_function
中:归功于 @Janusz Skonieczny 的回答!
You only need:
if you already have SQL debug statements being printed in
runserver
.Add the decorator to your
class TestA(TestCase)
ortest_function
:Credits to @Janusz Skonieczny's answer!
在现代 Django 中,我们有挂钩和检测 sql 查询的本机方法无需安装额外的依赖项。 OP 要求提供所有查询,但这也可能具有很高的情境用途。
In modern Django we got native way to hook and instrument sql queries without need to install extra dependencies. The OP asked for all queries, but this also can be of a high situational use.
如果您想通过设置来切换此功能,请在 settings.py 中执行类似以下操作:
另外,请注意,只有在 settings.py 中有
DEBUG = True
时,这才有效。感谢 @Gian Marco 提供的日志配置使这项工作得以实现。
If you want to have this toggle-able by a setting, do something like the following in settings.py:
Also, please note that this will only work if you have
DEBUG = True
in settings.py.Thanks to @Gian Marco for the logging config that makes this work.
我不知道如何将 Django 中的所有 SQL 查询记录到文件中。
但是,我知道如何使用下面的代码在 Django Admin 中获取部分 SQL 查询。 *您还可以查看我的回答解释了如何在Django视图中获取SQL查询的部分:
例如,您可以使用
来获取< strong>SQL 查询如下所示:
Person
admin 中重写save_model()
中的connection.queries然后,如果您更改一个人,如下所示:
SQL 查询打印在控制台上,如下所示:
I don't know how to log all SQL queries in Django to a file.
But, I know how to use the code below to get the part of the SQL queries in Django Admin. *You can also see my answer explaining how to get the part of the SQL queries in Django View:
For example, you can use
connection.queries
in overriddensave_model()
inPerson
admin to get the SQL queries as shown below:Then, if you change a person as shown below:
The SQL queries are printed on console as shown below:
您需要将其放入中间件包中。中间件位于 webserver/django 核心和所有视图之间。它可以在请求之前进行预处理,并在请求完成之后进行后处理。例如,将查询保存到文件中。
You need to put this in a middleware package. The middleware sits between the webserver/django core and all your views. It can do preprocessing before the request, and postprocessing after the request completed. For example, save the queries to a file.