GeneralBear
GeneralBear

Reputation: 1021

Update multiple rows with Django (different ids) using different values for each id (as keys)

Let's say I have a model:

I have some items:

grocery = {
   status : True,
   fruits: ['1', '23', '55'],
   comments: {'1': "test", '23': "test2", '55': ""}

I have a rough potential Django update query:

Fruit.objects.all().filter(id__in=grocery.get('fruits')).update(status=grocery.get('status'), comment=grocery.get('comments'))

I'm successfully updating the status but I want to dynamically update the comments so that if the Fruit object in question has, for example, id: 23, the fruit comment will be test2, or if the object has 'id: 55' it will be ''

Upvotes: 0

Views: 1478

Answers (1)

cedric
cedric

Reputation: 302

Not sure of the exact syntax but i think of something like this:

for f in Fruit.objects.filter(id__in=grocery.get('fruits')) :
    f.update(status=grocery.get('status'), comment=grocery['comments'][f.id])

Upvotes: 1

Related Questions