开发者

Django partial update

开发者 https://www.devze.com 2023-04-07 04:18 出处:网络
I have two threads, one which runs s开发者_JS百科omething like update t set ColA=foo and the other runs update t set ColB=foo. If they were doing raw SQL statements there would be no contention, but s

I have two threads, one which runs s开发者_JS百科omething like update t set ColA=foo and the other runs update t set ColB=foo. If they were doing raw SQL statements there would be no contention, but since Django gets and saves the entire row, a race condition can occur.

Is there any way to tell Django that I just want to save a certain column?


Update old topic.

Now, we have update_fields argument with save:

If save() is passed a list of field names in keyword argument update_fields, only the fields named in that list will be updated.

https://docs.djangoproject.com/en/stable/ref/models/instances/#specifying-which-fields-to-save

product.name = 'Name changed again'
product.save(update_fields=['name'])


You are correct that save will update the entire row but Django has an update which does exactly what you describe.

https://docs.djangoproject.com/en/stable/ref/models/querysets/#update


I think your only option to guarantee this is to write the raw SQL by hand by using Manager.raw() or a cursor depending on which one is more appropriate.

0

精彩评论

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