How to update fields in a model without creating a new record in django?

后端 未结 5 1679
你的背包
你的背包 2020-12-23 11:14

I have a model in django that I want to update only, that is, when I call it and set the data, it will not create a new record, only update the existing one. How can I do th

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-23 11:54

    Sometimes it may be required to execute the update atomically that is using one update request to the database without reading it first.

    Also get-set attribute-save may cause problems if such updates may be done concurrently or if you need to set the new value based on the old field value.

    In such cases query expressions together with update may by useful:

    TemperatureData.objects.filter(id=1).update(value=F('value') + 1)
    

提交回复
热议问题