如何根据'统计学生总数;课程';django模型

如何根据'统计学生总数;课程';django模型,django,Django,我正试图根据CourseMasterModel计算学生人数。 我是用MySQL做的,但我想用Django 从课程主控表中选择cn.课程名称、计数(st.id) cn、semister\u master sem、di分部\u master di、student\u profile st何处 st.division_id=di.id和di.semister_id=sem.id和sem.course_id= cn.id按cn.course\u名称分组 您可以为您的CourseMasterModel添

我正试图根据CourseMasterModel计算学生人数。 我是用MySQL做的,但我想用Django

从课程主控表中选择cn.课程名称、计数(st.id) cn、semister\u master sem、di分部\u master di、student\u profile st何处 st.division_id=di.id和di.semister_id=sem.id和sem.course_id= cn.id按cn.course\u名称分组



您可以为您的
CourseMasterModel
添加注释,如:

from django.db.models import Count

CourseMasterModel.objects.annotate(
    nstudents=Count('semistermastermodel__devisionmastermodel__studentprofilemodel')
)
from django.db.models import Count

CourseMasterModel.objects.annotate(
    nstudents=Count('semistermastermodel__devisionmastermodel__studentprofilemodel')
)
from django.db.models import Count

CourseMasterModel.objects.annotate(
    nstudents=Count('semistermaster__devisionmaster__studentprofile')
)