[Django]-Can I do custom complicated group by in a Django QuerySet?

9👍

You can use the extra method to add in the year and date values before doing the aggregation.

Somewhere.objects.extra(select={'year': 'EXTRACT(year FROM date)',
                                'month': 'EXTRACT(month FROM date)'}
                       ).values_list('year', 'month').annotate(Sum('amount'))

1👍

Leave a comment