如何将Django查询集中的实例总数附加到该查询集中? [英] How append sum of instances within a django queryset to that queryset?
本文介绍了如何将Django查询集中的实例总数附加到该查询集中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的Django Queryset对象(它是派生的查询集,而不是模型的查询集):
I have a Django Queryset object that looks like this (it is a derived queryset, not a queryset for a Model):
<QuerySet [{'A': 2, 'B':3, 'C':0 }, {'A': 1, 'B':2, 'C':1 }]>
如何将包含查询集中实例总数的新实例附加到该查询集中?也就是说,我需要创建一个新的(或相同的)查询集,如下所示:
How can I append a new instance containing sum of instances within the queryset to that queryset? That is, I need to make a new (or the same) queryset that looks like this:
<QuerySet [{'A':2, 'B':3, 'C':0 }, {'A':1, 'B':2, 'C':1 },
{'A':3, 'B':5, 'C':1 }]>
推荐答案
我认为您无法做到。但是,如果您使用 聚合
,那么您将能够获得 A
, B
, C
,像这样:
I don't think you can do that. But if you use aggregate
then you will be able to get the sum of A
, B
, C
like this:
>> result = YourModel.objects.aggregate(A=Sum('A'), B=Sum('B'), C=Sum('C'))
>> print(result)
这篇关于如何将Django查询集中的实例总数附加到该查询集中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文