Можете группировать на стороне БД
Примерно так
from app import models
from django.contrib.postgres.aggregates import ArrayAgg
from collections import defaultdict
result = defaultdict(dict)
query = models.Product.objects.values(
'sku',
'plu',
).annotate(
pack_types=ArrayAgg('pack_type_id')
)
for row in query:
result[row['sku']][row['plu']] = row['pack_types']
INSERT INTO public.product (sku,plu,pack_type_id) VALUES
(1,'10',2),
(1,'10',3),
(1,'11',4),
(2,'10',5);
Результат:
{
"1": {
"10": [
2,
3
],
"11": [
4
]
},
"2": {
"10": [
5
]
}
}