user1624522 user1624522 - 1 year ago 126
Python Question

generate update query using django orm

I need to implement this query using django orm:

update table set field=field+1 where id=id

I don't whant to use this:

o = model.objects.get(id=id)

because it use select and when update, and not thread safe.

How to implement this via orm?

Answer Source

Both the previous answerers have part of the solution: you should use update in conjunction with F():

Model.objects.filter(id=id).update(field=F('field') +1))

Note this does an in-place UPDATE without any need for SELECT at all.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download