為了簡化操作,我需要執行此查詢: select state,count(*) from Airports group by state order by count(*) desc從我的查詢中返回的所需結果是這樣的字典:{ 'state1': value1, 'state2': value2, 'state3': value3, ... 'staten': valuen,}我做了一些研究,似乎我需要使用聚合和注釋,但我有點迷失在如何使用values_list()執行此操作。我可以像這樣在里面使用計數嗎?Airport.objects.values_list('state', Airport.objects.count()).annotate('state').order_by(-Airport.objects.count())
1 回答

猛跑小豬
TA貢獻1858條經驗 獲得超8個贊
from django.db.models import Count Airport.objects.values('state').annotate(count=Count('state')).order_by('-count')
添加回答
舉報
0/150
提交
取消