[Answered ]-Counting and summing values of records, filtered by a dictionary of foreign keys in Django

2👍

Count all tasks that have task_type by the name of “XYZ”, limited to clients in queryset

Task.objects.filter(client__in=clients, task_type__name='XYZ').count()

Sum all the value_num for all the tasks by the name of “ABC”, limited to clients in the queryset.

from django.db.models import Sum

Task.objects.filter(client__in=clients, task_type__name='ABC').annotate(Sum('task_value__value_num'))

Leave a comment