From 21fe7f66cdb83898d785038f8290f910fb383273 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Sun, 28 Jan 2024 23:54:55 -0500 Subject: [PATCH] membershipworks: Expose EventExtQuerySet methods on EventExtManager --- membershipworks/models.py | 45 ++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/membershipworks/models.py b/membershipworks/models.py index 9079164..12da651 100644 --- a/membershipworks/models.py +++ b/membershipworks/models.py @@ -448,32 +448,37 @@ 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( - 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") - .annotate(d=Sum("duration")) - .values("d")[:1], - output_field=models.DurationField(), - ), - person_hours=ExpressionWrapper( - ExpressionWrapper(F("duration"), models.IntegerField()) * F("count"), - models.DurationField(), - ), + return ( + super() + .get_queryset() + .annotate( + 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") + .annotate(d=Sum("duration")) + .values("d")[:1], + output_field=models.DurationField(), + ), + person_hours=ExpressionWrapper( + ExpressionWrapper(F("duration"), models.IntegerField()) + * F("count"), + models.DurationField(), + ), + ) ) class EventExt(Event): """Extension of `Event` to capture some fields not supported in MembershipWorks""" - objects = EventExtManager() + objects = EventExtManager.from_queryset(EventExtQuerySet)() instructor = models.ForeignKey( EventInstructor, on_delete=models.PROTECT, null=True, blank=True