在Django Shell会话期间获取SQL查询计数 [英] Get SQL query count during a Django shell session
本文介绍了在Django Shell会话期间获取SQL查询计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以打印在Django shell会话期间Django ORM执行的原始SQL查询的数量?
Is there a way to print the number of raw SQL queries performed by the Django ORM during a Django shell session?
Django调试工具栏(例如,5.83MS中的 5个查询 $ c,但是如何从shell中获取它并不清楚。
This sort of information is already provided by the Django debug toolbar (e.g, 5 QUERIES in 5.83MS
but it's not obvious how to get it from the shell.
推荐答案
您可以使用 connection.queries
:
>>> from django.conf import settings
>>> settings.DEBUG = True
>>> from django.db import connection
>>> Model.objects.count()
>>> # python 3 uses print()
>>> print(len(connection.queries))
1
这篇关于在Django Shell会话期间获取SQL查询计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文