[Django]-How to group by with annotate in django?

2👍

You can work with the ArrayAgg aggregate [Django-doc] to obtain a list of items:

from django.contrib.postgres.aggregates import ArrayAgg

QuizzUserItem.objects.filter(
    examuser=user_exam
).values(
    'item__question'
).annotate(
    items=ArrayAgg('item')
).order_by('item__question')

Leave a comment