使用 django.db.connection.queries [英] Using django.db.connection.queries

查看:33
本文介绍了使用 django.db.connection.queries的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

只要 DEBUG 为 True.

as long as DEBUG is True.

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

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 ...

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

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

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

Any ideas why connection.queries is always empty?

推荐答案

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

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.

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

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

这篇关于使用 django.db.connection.queries的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆