Django Annotate Conditional
I need a list of users with the total fees they each have. I only want to include fees in the amount due. This returns the sum of all fees, it does not filter out charges that are not yet charged.
user_array = User.objects.all().annotate( total_sum=Sum('charge__amount', only=Q(charge__due_date__lte=date)) )
+3
source to share
1 answer
Try
user_array = User.objects.filter(charge__due_date__lte=date).annotate(amount_due=Sum('charge__amount'))
motivated by https://docs.djangoproject.com/en/1.8/topics/db/aggregation/#order-of-annotate-and-filter-clauses . The docs state that only invalidated relationship values ββwill be annotated.
+1
source to share