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

后端 未结 5 957
小鲜肉
小鲜肉 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:15

    And if you get this running, it isn't thread safe. While your update is running, some other process can update a model not knowing the data in the database is updated.

    You have too acquire a lock, but don't forget this senario:

    1. Django: m = Model.objects.all()[10]
    2. Django: m.field = field
    3. Django: a progress which takes a while (time.sleep(100))
    4. DB: Lock table
    5. DB: Update field
    6. DD: Unlock table
    7. Django: the slow process is finished
    8. Django: m.save()

    Now the field update became undone by the model instance in Django (Ghost write)

提交回复
热议问题