[Answered ]-Group by and return zero if no value is present in Django

1👍

Probably you can use Coalesce for this, for example:

from django.db.models import Value as V
from django.db.models.functions import Coalesce


def get_queryset(self):
    date = datetime.date.today()
    current_year = date.today().year
    queryset = Subscription.objects.filter(created__year=current_year)\
        .exclude(user__is_staff=True).values(
            month=TruncMonth('created')
        ).annotate(
            total_members=Coalesec(Count('created'), V(0))
        ).order_by('month')
    return queryset
👤ruddra

Leave a comment