diff --git a/tracker/models/event.py b/tracker/models/event.py index 478febab..6a222150 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 ), )