I’m trying to atomically increment a simple counter in Django. My code looks like this:
JavaScript
x
9
1
from models import Counter
2
from django.db import transaction
3
4
@transaction.commit_on_success
5
def increment_counter(name):
6
counter = Counter.objects.get_or_create(name = name)[0]
7
counter.count += 1
8
counter.save()
9
If I understand Django correctly, this should wrap the function in a transaction and make the increment atomic. But it doesn’t work and there is a race condition in the counter update. How can this code be made thread-safe?
Advertisement
Answer
Use an F expression:
JavaScript
1
2
1
from django.db.models import F
2
either in update()
:
JavaScript
1
3
1
Counter.objects.get_or_create(name=name)
2
Counter.objects.filter(name=name).update(count=F("count") + 1)
3
or on the object instance:
JavaScript
1
4
1
counter, _ = Counter.objects.get_or_create(name=name)
2
counter.count = F("count") + 1
3
counter.save(update_fields=["count"])
4
Remember to specify update_fields
, or you might encounter race conditions on other fields of the model.
A note on the race condition avoided by using F expressions has been added to the official documentation.