0

I'm facing some troubles by trying to make a like/unlike system for my app.

It's okay when i like/unlike in a normal timelapse, but when i click multiple times in a very short frequency (20 clicks in 10 seconds for example) then my counter is disturbed.

Normally while i'm testing my counter should always have a value of 1 or 0 since i am the only one to do the operation. However when i click quickly and multiple times, my rest api cannot follow the rythm and i'm getting a counter that have 7 or 8 likes.

I've tried multiple solutions. first by adding the model transaction included with django. As a decorator :

@transaction.atomic

Or manually :

     with transaction.atomic():
     #do stuff

I've also tried to increment directly from the database by using F() :

votes=F("votes") + 1

Nothing seems to work.

Any suggestion ?

John doe
  • 3,680
  • 7
  • 31
  • 65
  • don't allow more than one vote. No? – Adelin Feb 05 '18 at 11:54
  • Possible duplicate of [Django Vote Up/Down method](https://stackoverflow.com/questions/1528583/django-vote-up-down-method) – Adelin Feb 05 '18 at 11:55
  • how do you check if user already voted? – CrowbarKZ Feb 05 '18 at 13:30
  • votes=F("votes") + 1 does exactly what you need. The problem probably exists somewhere else. – kevswanberg Feb 05 '18 at 16:59
  • @kevswanberg Probably. i think when i test it my backend doesn't have the time to process before another request come. So it create a disturbing. No i need to find a way to freeze de request until the `HTTP 200 OK` come to my client. – John doe Feb 05 '18 at 17:03

0 Answers0