I want to get an output of {'month': 'jan', 'count': 1600} but I get the following with the current code {'month': 1, 'count': 1600}. How do I do this so I get the name of the month and not the integer.
class Month(Func):
function = 'EXTRACT'
template = '%(function)s(MONTH from %(expressions)s)'
output_field = IntegerField()
Note.objects.filter(
email_status__in=status_list,
email_template__isnull=False,
creation_time__gt=one_year
).annotate(
month=Month('creation_time')
).values('month').annotate(
total_count=Count(
'email_id', distinct=True)
).values('total_count', 'month')
You could try using DATE_FORMAT instead of EXTRACT. The following is untested.
class Month(Func):
function = 'EXTRACT'
template = "%(function)s(%(expressions)s, '%%b')"
output_field = CharField()
Alternatively, it shouldn't be too tricky to convert the integers 1-12 to 'jan'-'dec' in Python.