django:ORM之annotate的应用

annotate:聚合分组,对应sql语句的group by
第一步:导入
from django.db.models.aggregates import Count
第二步:应用
res = Students.objects.values(‘gender’).annotate(people=Count(‘gender’))
对应sql:
SELECT M3_students.gender, COUNT(M3_students.gender) AS people FROM M3_students GROUP BY M3_students.gender ORDER BY NULL

上一篇:多进程5


下一篇:DjangoRestFramework系列教程(3):基于类的视图层