From df621988f6ce0346971fb675be1483785fe7bbc2 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Fri, 19 Jan 2024 16:25:20 -0500 Subject: [PATCH] membershipworks: Fix bad grouping in EventExt.summarize using subquery --- membershipworks/models.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/membershipworks/models.py b/membershipworks/models.py index e84ac9b..8fc7dea 100644 --- a/membershipworks/models.py +++ b/membershipworks/models.py @@ -433,7 +433,7 @@ class EventExtQuerySet(models.QuerySet["EventExt"]): return method( count__sum=Sum("count", filter=F("occurred")), instructor__count=Count("instructor", distinct=True, filter=F("occurred")), - meeting_times__count=Count("meeting_times", filter=F("occurred")), + meeting_times__count=Count("meeting_times__count", filter=F("occurred")), duration__sum=Sum("duration", filter=F("occurred")), person_hours__sum=Sum("person_hours", filter=F("occurred")), event_count=Count("eid", filter=F("occurred")), @@ -444,7 +444,13 @@ class EventExtQuerySet(models.QuerySet["EventExt"]): class EventExtManager(models.Manager["EventExt"]): def get_queryset(self) -> models.QuerySet["EventExt"]: return EventExtQuerySet(self.model, using=self._db).annotate( - Count("meeting_times"), + meeting_times__count=Subquery( + EventMeetingTime.objects.filter(event=OuterRef("pk")) + .values("event__pk") + .annotate(d=Count("pk")) + .values("d")[:1], + output_field=models.IntegerField(), + ), duration=Subquery( EventMeetingTime.objects.filter(event=OuterRef("pk")) .values("event__pk")