I've already read this Django: Group by date (day, month, year) and all related stuff by googling "django group by month"
If I try the "cleanest" solution - using Django 1.11, I end up with this:
class Request(BaseModel):
date_creation = models.DateTimeField(default=None,
blank=True, null=True)
print([v for v in
Request.objects.annotate(month=ExtractMonth('date_creation'),
year=ExtractYear('date_creation'),)
.values('month', 'year')
.annotate(total=Count('month'))
.values('month', 'year', 'total')
])
And the result doesn't do a group by! I get this:
[{'month': 6, 'year': 2017, 'total': 1},
{'month': 7, 'year': 2017, 'total': 1},
{'month': 7, 'year': 2017, 'total': 1}]
I need to get:
[{'month': 6, 'year': 2017, 'total': 1},
{'month': 7, 'year': 2017, 'total': 2}]
I've also tried:
print([v for v in
Request.objects.extra({'month': 'strftime("%m", date_creation)',
'year': 'strftime("%Y", date_creation)'})
.values('month', 'year')
.annotate(total=Count('*'))
.values('month', 'year', 'total')
])
And then I get:
[{'month': '06', 'year': '2017', 'total': 1},
{'month': '07', 'year': '2017', 'total': 1},
{'month': '07', 'year': '2017', 'total': 1}]
Any idead?