membershipworks: Convert EventExt.duration annotation to a Subquery
should be somewhat less performant, but allows for easier aggregation
This commit is contained in:
parent
0633e4ecef
commit
b8c2792f0a
@ -4,7 +4,7 @@ from datetime import datetime
|
|||||||
import django.core.mail.message
|
import django.core.mail.message
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db import models
|
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
|
from django.utils import timezone
|
||||||
|
|
||||||
|
|
||||||
@ -415,7 +415,19 @@ class EventInstructor(models.Model):
|
|||||||
|
|
||||||
class EventExtManager(models.Manager["EventExt"]):
|
class EventExtManager(models.Manager["EventExt"]):
|
||||||
def get_queryset(self) -> models.QuerySet["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):
|
class EventExt(Event):
|
||||||
|
Loading…
Reference in New Issue
Block a user