I didn’t know how to title this question, honestly. Suppose a view like this below. My users can have maximum 10 dollars in their pocket. I’ve added a button that hits this view, and adds 1 dollar at time until 10 is reached. This is just an example.
def my_view(request): money_to_add = 10 - request.user.money for i in range(0, money_to_add): user.money += 1 request.user.save() return HttpResponse(“Ok!”)
Problem is that if the user clicks very fast the button two times, then ‘10 - request.user.money’ is added twice to the account.
How can i solve this problem, just modifying the view? I’d prefer avoid to use external packages or modify the model’s behavior.
EDIT: Using class based views is ok! I just used a fbv for example to make it clear.