一.参考资料
Django官网:https://docs.djangoproject.com/en/2.1/topics/db/aggregation/
二.aggregate()
aggregate()为所有的QuerySet生成一个汇总值,相当于Count()。返回结果类型为Dict。
示例:
n [4]: from django.db.models import Sum
In [5]: today_revenue = Book.objects.filter(today_revenue__gt=0).aggregate(today_revenue=Sum('today_revenue'))
In [6]: type(today_revenue)
Out[6]: dict
三.annotate()
annotate()为每一个QuerySet在指定属性上生成汇总值,相当于GROUP BY。返回结果类型QuerySet。