简体   繁体   English

append 如何将 django 查询集中的实例总和到该查询集?

[英]How append sum of instances within a django queryset to that queryset?

I have a Django Queryset object that looks like this (it is a derived queryset, not a queryset for a Model):我有一个 Django 查询集 object 看起来像这样(它是派生的查询集,而不是模型的查询集):

<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?我怎样才能 append 一个包含查询集中实例总和的新实例到该查询集? 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 }]> 

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:但是,如果您使用aggregate ,那么您将能够得到ABC的总和,如下所示:

>> result = YourModel.objects.aggregate(A=Sum('A'), B=Sum('B'), C=Sum('C'))
>> print(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM