How to fill empty months using Django queryset

16 views
Skip to first unread message

Charles Sartori

unread,
Jul 24, 2018, 4:14:33 PM7/24/18
to Django users
I have the following:

In [39]: from django.db.models.functions import TruncDate
    ...: p = Purchase.objects \
    ...:     .annotate(date=TruncDate('event_date')) \
    ...:     .values('date') \
    ...:     .annotate(total=Sum('quantity')) \
    ...:     .order_by('event_date')
    ...: 
    ...: 

In [40]: p
Out[40]: <PolymorphicQuerySet [{'date': datetime.date(2017, 1, 1), 'total': 10.0}, {'date': datetime.date(2018, 1, 1), 'total': 11.0}, {'date': datetime.date(2018, 7, 24), 'total': 10.0}]>

Is it possible to fill the empty months between the first and the last month with default value using the ORM? Something like {'date': DATETIME_OBJ, 'total': 0}...
Reply all
Reply to author
Forward
0 new messages