Django .annotate TruncMonth : How to Get full range of Months

Viewed 148

I want total earning from each month and display it in graph so im making below query

Here's my query:

        query = UserOrderService.objects.all()

        query = query.annotate(month=TruncMonth('order_booking_date')).values(
            'month').annotate(no_of_services=Count('pk')).annotate(
            total=Sum(F('service_price') * F('quantity'))).order_by('month')

Above query only gives data from months where booking happend

Output:

"data": [
    {
        "no_of_services": 1,
        "total": 26725.0,
        "month": "November"
    },
    {
        "no_of_services": 1,
        "total": 26725.0,
        "month": "December"
    }
]

Where as i want all data from all months Even if there's no data in that particular month

Expected Output:

"data": [
    {
        "no_of_services": 0,
        "total": 0,
        "month": "January"
    },
    {
        "no_of_services": 0,
        "total": 0,
        "month": "February"
    },
    {
        "no_of_services": 0,
        "total": 0,
        "month": "March"
    },
    
    ...

    {
        "no_of_services": 1,
        "total": 26725.0,
        "month": "November"
    },
    {
        "no_of_services": 1,
        "total": 26725.0,
        "month": "December"
    }
]
0 Answers
Related