From 6d1e2a3ae02cf44fcf423fe98498cd20a0befb5b Mon Sep 17 00:00:00 2001 From: Benjamin Cutler Date: Sun, 5 Jan 2025 16:30:28 -0700 Subject: [PATCH] fix event annotations for postgres (#757) [#188745145] --- tracker/models/event.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/tracker/models/event.py b/tracker/models/event.py index 478febab1..6a222150f 100644 --- a/tracker/models/event.py +++ b/tracker/models/event.py @@ -2,6 +2,7 @@ import decimal import itertools import logging +from decimal import Decimal import post_office.models from django.contrib.auth.models import User @@ -9,7 +10,7 @@ from django.core.validators import validate_slug from django.db import models from django.db.models import Case, Count, F, Q, Sum, When, signals -from django.db.models.functions import Cast, Coalesce +from django.db.models.functions import Coalesce from django.dispatch import receiver from django.urls import reverse from timezone_field import TimeZoneField @@ -59,23 +60,20 @@ def current_or_next(self, timestamp=None): def with_annotations(self, ignore_order=False): annotated = self.annotate( - amount=Cast( - Coalesce( - Sum( - Case( - When( - Q(donation__transactionstate='COMPLETED'), - then=F('donation__amount'), - ), - output_field=models.DecimalField(decimal_places=2), - ) - ), - 0.0, + amount=Coalesce( + Sum( + Case( + When( + Q(donation__transactionstate='COMPLETED'), + then=F('donation__amount'), + ), + output_field=models.DecimalField(decimal_places=2), + ) ), - output_field=models.DecimalField(), + Decimal('0.00'), ), - donation_count=Count( - 'donation', filter=Q(donation__transactionstate='COMPLETED') + donation_count=Coalesce( + Count('donation', filter=Q(donation__transactionstate='COMPLETED')), 0 ), )