使用 django.db.connection.queries

发布于 2024-08-19 06:35:11 字数 538 浏览 5 评论 0原文

我有一个 Python/Django 应用程序,它运行大量 SQL 语句。出于调试目的,我认为应该为我创建一个简单的视图,其中仅列出已运行的所有 SQL 语句。

根据文档,这段代码应该足以做到这一点:

    from django.db import connection
    connection.queries

只要 DEBUG 为 True。

然而,这并没有给我任何东西。 DEBUG 肯定设置为 True。这个connection.queries 存储在什么上下文中?我的意思是,我应该能够执行一个执行大量 SQL 语句的页面,然后切换到 http:// /myserver/sql 我创建的视图并看到那些 SQL 语句,对吗?当然,使用相同的浏览器会话...

我确实检查了 db.reset_queries() 是否在代码中的任何位置运行,似乎没有。

知道为什么connection.queries 总是空的吗?

I've got a Python/Django application which runs quite a lot of SQL statements. For debugging purposes, I thought I should create a simple view for me which just lists all the SQL statements that have been run.

According to the documentation, this code should be enough to do that:

    from django.db import connection
    connection.queries

as long as DEBUG is True.

However, this is not giving me anything. DEBUG is most certainly set to True. In what context is this connection.queries stored? I'm mean, I should be able to execute one page which executes a lot of SQL statements, and then just switch over to the http://myserver/sql view I created and see those SQL statements there, right? Using the same browser session of course ...

I did check if db.reset_queries() was being run anywhere in the code, appears it's not.

Any ideas why connection.queries is always empty?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一生独一 2024-08-26 06:35:11

Ben 是对的,您只能看到来自当前进程的查询。您可以在同一视图或控制台中使用它,但不能在视图之间使用它。

查看视图中正在执行哪些查询的最佳方法是使用 Django 调试工具栏

Ben is right that you only see queries from the current process. You can use it within the same view, or in the console, but not between views.

The best way to see what queries are executing in your views is to use the Django debug toolbar.

爱你不解释 2024-08-26 06:35:11

@Daniel Roseman 这是一个好主意,但如果你想了解开箱即用的 sql 查询:

安装 django-command-extensions 并将其添加到已安装的应用程序中。
它将向您的项目添加许多 utils 命令,其中之一:

  • debugsqlshell:输出当您在 Python 交互式 shell 中工作时执行的 SQL。

例子:
python manage.py debugsqlshell

In [1]:from django.contrib.auth.models import User
In [1]:User.objects.all()

Out[2]: SELECT "auth_user"."id",
   "auth_user"."username",
   "auth_user"."first_name",
   "auth_user"."last_name",
   "auth_user"."email",
   "auth_user"."password",
   "auth_user"."is_staff",
   "auth_user"."is_active",
   "auth_user"."is_superuser",
   "auth_user"."last_login",
   "auth_user"."date_joined"
    FROM "auth_user" LIMIT 21  [1.25ms]

@Daniel Roseman its a good idea, but if you want to know sql queries out of the box:

install django-command-extensions and add it to installed apps.
it will add many utils commands into your project, one of them:

  • debugsqlshell: Outputs the SQL that gets executed as you work in the Python interactive shell.

example:
python manage.py debugsqlshell

In [1]:from django.contrib.auth.models import User
In [1]:User.objects.all()

Out[2]: SELECT "auth_user"."id",
   "auth_user"."username",
   "auth_user"."first_name",
   "auth_user"."last_name",
   "auth_user"."email",
   "auth_user"."password",
   "auth_user"."is_staff",
   "auth_user"."is_active",
   "auth_user"."is_superuser",
   "auth_user"."last_login",
   "auth_user"."date_joined"
    FROM "auth_user" LIMIT 21  [1.25ms]
一花一树开 2024-08-26 06:35:11

我认为这些查询存储在内存中,并且不在进程之间共享,因此您只能访问当前进程发出的查询。

如果我尝试您在 ./manage.py shell 会话中粘贴的代码,我只会看到我之前在该 shell 会话中进行的查询。

如果我将查询从视图传递到模板上下文并在模板中显示它,我只会看到在该视图中进行的查询。不过,这是使用开发服务器。

我假设(但尚未测试)如果您在一个进程服务多个请求的环境中使用它,您会看到每个请求保存更多查询。

I think these queries are stored in memory, and not shared between processes, so you will only have access to the queries made by the current process.

If I try the code that you pasted in a ./manage.py shell session, I only see queries I've previously made in that shell session.

If I pass queries from a view into a template context and show it in the template, I see just the queries made in that view. This is using the dev server, though.

I assume—but haven't tested—that if you use this in an environment where you have one process serving multiple requests, you would see more queries being saved each request.

隔岸观火 2024-08-26 06:35:11
from django.db import connections
x = connections['rating']
x.queries

So check another connections!
from django.db import connections
x = connections['rating']
x.queries

So check another connections!
不如归去 2024-08-26 06:35:11

这就是为我解决的问题;我用:

reduce(lambda n, name: n + connections[name].queries, connections, 0)

来获取查询计数。

That is what fixed it for me; I used:

reduce(lambda n, name: n + connections[name].queries, connections, 0)

to get the query count.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文