Django: Using an F expression for a text field in an update call

后端 未结 5 981
小鲜肉
小鲜肉 2021-01-01 10:17

In a django view, I need to append string data to the end of an existing text column in my database. So, for example, say I have a table named \"ATable\", and it has a fiel

5条回答
  •  自闭症患者
    2021-01-01 11:21

    You can achieve this functionality with Django's select_for_update() operator: https://docs.djangoproject.com/en/dev/ref/models/querysets/#select-for-update

    Something like this:

    obj = ATable.objects.select_for_update().get(id=100)
    obj.aField = obj.aField + aStringVar
    obj.save()
    

    The table row will be locked when you call .select_for_update().get(), and the lock will be released when you call .save(), allowing you to perform the operation atomically.

提交回复
热议问题