views:

32

answers:

1

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

to_update = [{'id':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 thant i can do this in a better way, but i didn't find it. Thank You

A: 

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.

Ignacio Vazquez-Abrams
but how do i do a multiple update with diferent values and different ids?
sacabuche
You can't. Just like you can't do it in SQL.
Ignacio Vazquez-Abrams
Manual management of database transactions (`@commit_manually` and so on) could result in speed boost in some environments.
gorsky