Django(22)聚合查询

使用aggregate()过滤器调用聚合函数。聚合函数包括:Avg 平均,Count 数量,Max 最大,Min 最小,Sum 求和,
被定义在django.db.models中。

# 例:查询学生的平均年龄。
from django.db.models import Sum,Count,Avg,Max,Min

Student.objects.aggregate(Avg('age'))

aggregate的返回值是一个字典类型,格式如下:

{'属性名__聚合类小写':值}

使用count时一般不使用aggregate()过滤器。

Student.objects.count() # count函数的返回值是一个数字。
posted @ 2021-12-02 20:21  下个ID见  阅读(31)  评论(0)    收藏  举报