[Answered ]-Django AJAX increment

2👍

Remember to do it atomicly:

# models.py
class Page(models.Model):
    hits = models.PositiveIntegerField()

# views.py
def hit(request, page_pk):
    Page.objects.filter(pk=page_pk).update(hits=F('hits')+1)
    return HttpResponse()

…or use a transaction:

# views.py
from django.db.decorators import commit_on_success

@commit_on_success
def hit(request, page_pk):
    page = Page.objects.filter(pk=page_pk)
    page.hits += 1
    page.save()
    return HttpResponse()

Leave a comment