From ca3ba7507a31809ba193eeee68c8544679f08ddf Mon Sep 17 00:00:00 2001 From: juuso-j Date: Tue, 16 Apr 2024 10:44:32 +0300 Subject: [PATCH] Add get_sum_of_count function --- profiles/models.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/profiles/models.py b/profiles/models.py index adbded3..3f43c45 100644 --- a/profiles/models.py +++ b/profiles/models.py @@ -1,4 +1,5 @@ from django.db import models +from django.db.models import Sum class Question(models.Model): @@ -224,3 +225,10 @@ def __str__(self): class CumulativeResultCount(Result): class Meta: proxy = True + + def get_sum_of_count(self, postal_code_type_name=PostalCodeType.HOME_POSTAL_CODE): + postal_code_type = PostalCodeType.objects.get(type_name=postal_code_type_name) + qs = PostalCodeResult.objects.filter( + postal_code_type=postal_code_type, result=self + ) + return qs.aggregate(sum_of_count=Sum("count"))["sum_of_count"]