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

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

问题描述

我有一个Python / Django应用程序,它运行着相当多的SQL语句。为了进行调试,我以为我应该为我创建一个简单的视图,只列出所有已经运行的SQL语句。



根据文档,这段代码应该是足够这样做:

 从django.db导入连接
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屋!

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