djangoormdjango-2.1

Updates doesn't take affect in django orm, Is there something wrong in my code?


I need to update multiple rows in a url_field in one of my models, each product_id has its own corresponding new values to be loading. What's wrong with my code? No errors are returned.

idlist= ["",""]
url = ["https://www.sample.com","https://www.sample2.com"]
i = 0


while i < len(item_idlist):
Model.objects.filter(item_id=idlist[i].update(product_url=url[i]))
i += 1

I expect that every iterations will update my data inside the model.


Solution

  • You could use a for loop combined with zip:

    for id, url in zip(idlist, urls):
         m = Model.objects.filter(item_id=id)
         m.update(product_url = url)