From 47a12abb949076748cc2755217451c9df3f7d200 Mon Sep 17 00:00:00 2001 From: Alec Li Date: Mon, 21 Oct 2024 16:54:01 -0700 Subject: [PATCH] Fix queries for counts in course data export --- csm_web/scheduler/admin.py | 4 +++- csm_web/scheduler/views/export.py | 8 +++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/csm_web/scheduler/admin.py b/csm_web/scheduler/admin.py index 4a64342f..41806e58 100644 --- a/csm_web/scheduler/admin.py +++ b/csm_web/scheduler/admin.py @@ -715,7 +715,9 @@ def get_fields(self, request, obj=None): @admin.display(description="Section count") def get_section_count(self, obj: Course): """Retrieve the number of sections associated with this course.""" - return obj.mentor_set.count() # one-to-one between mentor objects and sections + # only count mentors that have a section associated with it; + # at most one section can be associated with a given mentor object + return obj.mentor_set.filter(~Q(section=None)).count() @admin.display(description="Student count") def get_student_count(self, obj: Course): diff --git a/csm_web/scheduler/views/export.py b/csm_web/scheduler/views/export.py index ea055df2..916e1840 100644 --- a/csm_web/scheduler/views/export.py +++ b/csm_web/scheduler/views/export.py @@ -365,18 +365,20 @@ def prepare_course_data( export_headers.append("Course title") if "num_sections" in fields: course_queryset = course_queryset.annotate( - num_sections=Count("mentor__section") + num_sections=Count("mentor__section", distinct=True) ) export_fields.append("num_sections") export_headers.append("Number of sections") if "num_students" in fields: course_queryset = course_queryset.annotate( - num_students=Count("mentor__section__students") + num_students=Count("mentor__section__students", distinct=True) ) export_fields.append("num_students") export_headers.append("Number of students") if "num_mentors" in fields: - course_queryset = course_queryset.annotate(num_mentors=Count("mentor")) + course_queryset = course_queryset.annotate( + num_mentors=Count("mentor", distinct=True) + ) export_fields.append("num_mentors") export_headers.append("Number of mentors")