mysql - django sql thread safe? -
assume have code:
product = product.objects.get(name='something') product.number_sold += 1 product.save()
if during query number_sold 10, , before save(), code run @ same time query return 10 again, means save number_sold = 11 twice ? in other words, can 2 users running django view can same value number_sold field ?
to avoid race condition in multi-thread/process code should use f()-expressions
:
from django.db.models import f product = product.objects.get(name='something') product.number_sold = f('number_sold') + 1 product.save()
Comments
Post a Comment