Django和Aggregate:不同值的总和? [英] Django and Aggregate: Sum of distinct values?

查看:121
本文介绍了Django和Aggregate:不同值的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行django聚合函数,但无法产生所需的结果。

I am trying to do a django aggregate function, but am unable to produce the desired result.

我所拥有的:

income_posts.values_list('category__name','amount')
[(u'Donation', Decimal("2000.00")), (u'Paycheck', Decimal("1200.00")), (u'Donation', Decimal("1000.00"))]

所需结果:

[(u'Donation', Decimal("3000.00")), (u'Paycheck', Decimal("1200.00))]

我需要对具有以下内容的金额字段求和

I need to Sum the 'amount' fields that have the same category__name.

推荐答案

Django 3.0在Sum和Avg上引入了 distinct = True:
https://docs.djangoproject.com/en/3.0/ref/models/querysets /#sum

Django 3.0 introduced "distinct=True" on Sum and Avg: https://docs.djangoproject.com/en/3.0/ref/models/querysets/#sum

这篇关于Django和Aggregate:不同值的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆