使用django.db.connection.queries [英] Using django.db.connection.queries
问题描述
我有一个运行相当多SQL语句的Python / Django应用程序。为了调试的目的,我想我应该为我创建一个简单的视图,只列出所有已运行的SQL语句。
根据文档,此代码应该足够这样做:
来自django.db import connection
connection.queries
只要DEBUG为True。
任何东西。 DEBUG最肯定设置为True。在什么上下文中是这个connection.queries存储?我的意思是,我应该能够执行一个页面执行很多SQL语句,然后只需切换到 http: // myserver / sql 视图我创建并看到那些SQL语句,对不对?使用相同的浏览器会话当然...
我检查了db.reset_queries()是否在代码中的任何地方运行,看起来不是。
任何想法为什么connection.queries总是空的?
解决方案Ben是对的您只能看到当前进程的查询。
查看在视图中执行的查询的最好方法是使用 Django调试工具栏。
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?
解决方案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.
这篇关于使用django.db.connection.queries的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!