怎么显示sql django运行? [英] How to show the sql django runs?

查看:153
本文介绍了怎么显示sql django运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,所以现在有一个这样的问题,答案是:

 >>> from django.db import connection 
>>>> connection.queries
[{'sql':'SELECT polls_polls.id,polls_polls.question,polls_polls.pub_date FROM polls_polls',
'time':'0.002'}]

问题是,我不知道这是什么意思。我可以看到它正在从一个控制台运行,但是我的django项目,好的,它作为一个django项目运行!



我在哪里放这个代码?在html视图中?在view.py?在model.py?



谢谢!

解决方案

在调试模式下, a href =http://pypi.python.org/pypi/django-debug-toolbar =nofollow> django-debug-toolbar ,它打印所有执行的SQL以及被调用者,并解释的SQL,以及额外的信息,如模板调用,请求,信号等。非常方便。



在shell中,正如Yuji Tomita建议的, print queryset.query (在旧版Django中,它类似于 queryset._get_sql_clause(),只是FYI)或连接。在您的问题中查询


Yep, so there is a question like this on SO already, with the answer:

>>> from django.db import connection
>>> connection.queries
[{'sql': 'SELECT polls_polls.id,polls_polls.question,polls_polls.pub_date FROM polls_polls',
'time': '0.002'}]

the problem is, i have no idea what that means. I can see it is being run from a console, but my django project, well, it runs as a django project!

Where do i put this code? In the html view? In the view.py? In the model.py?

thanks!

解决方案

In debug mode, you could use django-debug-toolbar, it prints all SQLs executed, as well as callees and explains of the SQLs, and extra info such as templates invoking, request, signals, etc. Very handy indeed.

In shell, as Yuji Tomita suggests, print queryset.query (in legacy Django it's something like queryset._get_sql_clause(), just FYI) or connection.queries in your question.

这篇关于怎么显示sql django运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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