开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜