Atomic increment of a counter in django

前端 未结 6 2054
暗喜
暗喜 2020-12-04 13:04

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         


        
6条回答
  •  独厮守ぢ
    2020-12-04 13:41

    Django 1.7

    from django.db.models import F
    
    counter, created = Counter.objects.get_or_create(name = name)
    counter.count = F('count') +1
    counter.save()
    

提交回复
热议问题