from django.db import connection, reset_queries
印刷: []
reset_queries()
p = XModel.objects.filter(id=id) \
.values('name') \
.annotate(quantity=Count('p_id'))\
.order_by('-quantity') \
.distinct()[:int(count)]
print(connection.queries)
虽然这会打印:
reset_queries()
tc = ZModel.objects\
.filter(id=id, stock__gt=0) \
.aggregate(Sum('price'))
print(connection.queries)
I have changed fields names to keep things simple. (Fields are of parent tables i.e. __
to multiple level)
I was trying to print MySQL queries that Django makes and came across connection.queries
, I was wondering why doesn't it prints empty with first, while with second it works fine. Although I am getting the result I expect it to. Probably the query is executed. Also am executing only one at a time.
正如已接受的答案所说您必须首先使用查询集因为它很懒(例如list(qs)
).
另一个原因可能是你必须处于DEBUG模式 (see FAQ):
connection.queries is only available if Django DEBUG setting is True
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)