I'm using Django 2.2 and I need to group a query set by month and count the number of rows
Input:
Id | Value | Date
1 | Example value 1 | 2020-02-10
2 | Example value 2 | 2020-02-17
3 | Example value 3 | 2020-03-21
4 | Example value 4 | 2020-04-15
Expected output
Month | Count
2020-02 | 2
2020-03 | 1
2020-04 | 1
I'm using an initial filtering like this.
details = Process.objects.filter(started_at__gte=start_date, finished_at__lte=end_date).order_by('-serial_number').distinct('serial_number')
#Some code using all details
#Filtering
After that I need to do the group by and count.
summary = (details
.annotate(m=Month('started_at'))
.values('m')
.annotate(total=Count('id'))
.order_by('id').distinct('m','id'))
But when I execute it I got the following error:
NotImplementedError: annotate() + distinct(fields) is not implemented.
Month class definition:
class Month(F):
function = 'EXTRACT'
template = '%(function)s(MONTH from %(expressions)s)'
output_field = models.IntegerField()
I'm trying to do something like this.