1

是的,所以已经有一个这样的问题,答案是:

>>> 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中?

谢谢!

4

1 回答 1

2

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.

于 2012-05-22T11:36:46.663 回答