[Answer]-Django ORM grouped by query

1πŸ‘

βœ…

I would say go with values_list combined with defaultdict, that way you don’t have to create model instances. QuerySets only return object instances or list instances.

d = defaultdict(list)
qset = ArmCodeMapping.objects.values_list('arm_id', 'code')
for item in qset:
    d[item[0]].append(item[1])

Now the variable d is filled with the result you want.

πŸ‘€Martin

Leave a comment