Django SUM查询?


Answers:


149

更新:以下内容合并了原始查询的ISNULL方面:

from django.db.models import Sum

ModelName.objects.filter(field_name__isnull=True).aggregate(Sum('field_name'))
# returns {'field_name__sum': 1000} for example

您正在寻找Sum聚合函数,其功能如下:

ModelName.objects.aggregate(Sum('field_name'))

参见:https : //docs.djangoproject.com/en/dev/ref/models/querysets/#sum


1
我认为您可以在查询中使用过滤器。我尚未测试过,但类似的方法应该起作用:ModelName.objects.filter(field_name__isnull=True).aggregate(Sum('field_name'))
滚石
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.