[Answered ]-Reduce time for Loop over multiple objects to get value Django

1👍

You can create a lookup dict with just one query. Then use that dict to find your description.

description_dict = {}
for product in Product.objects.values('short_code', 'description'):
    description_dict[product['short_code'] = product['description']

for i, v in df["D Code"].iteritems():
        product.append(description_dict[v[:2]])
        ...

Leave a comment