1👍
✅
It is not necessary to JOIN or merge, you can do this all in the same queryset:
from django.db.models import Count, Q
q_total = (
References.objects.values('date_received__year', 'date_received__month')
.annotate(
closed_count=Count('ref_no', filter=Q(status__iexact='CL')),
forward_count=Count('ref_no', filter=Q(status__iexact='FR')),
pending_count=Count('ref_no', filter=Q(status__iexact='PE')),
)
.order_by('date_received__year', 'date_received__month')
)
for each row it will thus have three attributes: .closed_count
, .forward_count
, .pending_count
.
Source:stackexchange.com