Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

python - 在Django Shell会话期间获取SQL查询计数(Get SQL query count during a Django shell session)

Is there a way to print the number of raw SQL queries performed by the Django ORM during a Django shell session?

(有没有一种方法可以打印在Django Shell会话期间Django ORM执行的原始SQL查询的数量?)

This sort of information is already provided by the Django debug toolbar (eg, 5 QUERIES in 5.83MS but it's not obvious how to get it from the shell.

(此类信息已由Django调试工具栏提供(例如, 5 QUERIES in 5.83MS但如何从shell中获取信息并不明显。)

  ask by Jian translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use connection.queries :

(您可以使用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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...