I\'m trying to atomically increment a simple counter in Django. My code looks like this:
from models import Counter
from django.db import transaction
@trans
Keeping it simple and building on @Oduvan's answer:
counter, created = Counter.objects.get_or_create(name = name,
defaults={'count':1})
if not created:
counter.count = F('count') +1
counter.save()
The advantage here is that if the object was created in the first statement, you don't have to do any further updates.