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.
精彩评论