其他分享
首页 > 其他分享> > django:ORM之annotate的应用

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

标签:Count,people,students,gender,django,annotate,ORM,M3
来源: https://blog.csdn.net/Mars_1GE/article/details/100830143