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.
- Django docs about
values_list. - Python docs about
defaultdict
π€Martin
Source:stackexchange.com