开发者

Efficient method to update one field with different values in a django model

开发者 https://www.devze.com 2022-12-21 10:11 出处:网络
I have many pk-value in a dictionary and I want to update the object with his new value. to_update = [{\'i开发者_如何学Pythond\':id1,\'value\':value1}, ... ]

I have many pk-value in a dictionary and I want to update the object with his new value.

to_update = [{'i开发者_如何学Pythond':id1,'value':value1}, ... ]

Now i'm doing this:

for t in to_update:
    Mymodel.objects.filter(pk=t['id']).update(myfield=t['value'])

I think that i can do this in a better way, but i didn't find it.


This is the most efficient way. QuerySet.update() is converted directly to an UPDATE statement, and there is no more efficient way of doing it than that.

0

精彩评论

暂无评论...
验证码 换一张
取 消