membershipworks: Convert EventExt.duration annotation to a Subquery

should be somewhat less performant, but allows for easier aggregation
This commit is contained in:
Adam Goldsmith 2024-01-17 13:25:37 -05:00
parent 0633e4ecef
commit b8c2792f0a

View File

@ -4,7 +4,7 @@ from datetime import datetime
import django.core.mail.message
from django.conf import settings
from django.db import models
from django.db.models import Exists, F, OuterRef, Sum
from django.db.models import Exists, F, OuterRef, Subquery, Sum
from django.utils import timezone
@ -415,7 +415,19 @@ class EventInstructor(models.Model):
class EventExtManager(models.Manager["EventExt"]):
def get_queryset(self) -> models.QuerySet["EventExt"]:
return super().get_queryset().annotate(duration=Sum("meeting_times__duration"))
return (
super()
.get_queryset()
.annotate(
duration=Subquery(
EventMeetingTime.objects.filter(event=OuterRef("pk"))
.values("event__pk")
.annotate(d=Sum("duration"))
.values("d")[:1],
output_field=models.DurationField(),
),
)
)
class EventExt(Event):