[Answered ]-Renaming a field on a Django queryset with group by

1👍

You can do this by using the name of your annotation, so:

return Response(User.objects.filter(
    date_joined__year=timezone.now().year
).values(
    month_id=TruncMonth('date_joined')
).annotate(qtd=Count('id')).order_by('month_id')

or if you want to extract the Month itself, you can work with ExtractMonth [Django-doc]

from django.db.models.functions import ExtractMonth

return Response(User.objects.filter(
    date_joined__year=timezone.now().year
).values(
    month_id=ExtractMonth('date_joined')
).annotate(qtd=Count('id')).order_by('month_id')

Leave a comment