From 35d8fec2a38c59675c71e11aec55636bb58d05c9 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Wed, 3 Jan 2024 14:25:04 -0500 Subject: [PATCH] membershipworks: Use GeneratedField for EventMeetingTime.duration Relevant Django bug was fixed in 5.0.1 https://code.djangoproject.com/ticket/35019 --- .../0007_eventmeetingtime_duration.py | 24 +++++++++++++++ membershipworks/models.py | 29 ++++--------------- 2 files changed, 30 insertions(+), 23 deletions(-) create mode 100644 membershipworks/migrations/0007_eventmeetingtime_duration.py diff --git a/membershipworks/migrations/0007_eventmeetingtime_duration.py b/membershipworks/migrations/0007_eventmeetingtime_duration.py new file mode 100644 index 0000000..59289af --- /dev/null +++ b/membershipworks/migrations/0007_eventmeetingtime_duration.py @@ -0,0 +1,24 @@ +# Generated by Django 5.0.1 on 2024-01-03 19:22 + +import django.db.models.expressions +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("membershipworks", "0006_eventext_instructor_flat_rate_and_more"), + ] + + operations = [ + migrations.AddField( + model_name="eventmeetingtime", + name="duration", + field=models.GeneratedField( + db_persist=False, + expression=django.db.models.expressions.CombinedExpression( + models.F("end"), "-", models.F("start") + ), + output_field=models.DurationField(), + ), + ), + ] diff --git a/membershipworks/models.py b/membershipworks/models.py index caf7acb..94a54c5 100644 --- a/membershipworks/models.py +++ b/membershipworks/models.py @@ -415,13 +415,7 @@ class EventInstructor(models.Model): class EventExtManager(models.Manager["EventExt"]): def get_queryset(self) -> models.QuerySet["EventExt"]: - return ( - super() - .get_queryset() - .annotate(duration=Sum(F("meeting_times__end") - F("meeting_times__start"))) - ) - # TODO: use simpler expression when GeneratedField fixed - # return super().get_queryset().annotate(duration=Sum("meeting_times__duration")) + return super().get_queryset().annotate(duration=Sum("meeting_times__duration")) class EventExt(Event): @@ -446,29 +440,18 @@ class EventExt(Event): verbose_name = "event" -class EventMeetingTimeManager(models.Manager["EventMeetingTime"]): - def get_queryset(self) -> models.QuerySet["EventMeetingTime"]: - return super().get_queryset().annotate(duration=F("end") - F("start")) - - class EventMeetingTime(models.Model): - objects = EventMeetingTimeManager() - event = models.ForeignKey( EventExt, on_delete=models.CASCADE, related_name="meeting_times" ) start = models.DateTimeField() end = models.DateTimeField() - # TODO: Should use generated field instead of manager, but this is - # broken due to current Django bug, pending next release (> 5.0) - # ref: https://code.djangoproject.com/ticket/35019 - - # duration = models.GeneratedField( - # expression=F("end") - F("start"), - # output_field=models.DurationField(), - # db_persist=False, - # ) + duration = models.GeneratedField( + expression=F("end") - F("start"), + output_field=models.DurationField(), + db_persist=False, + ) class Meta: constraints = [