-1

I'm using django (1.5 with mysql) select_for_update method for fetching data from one model and serve this data to user upon request, but when two user request at simultaneously it returns same data for both of the user, see the sample code below

models.py
class SaveAccessCode(models.Model):
    code = models.CharField(max_length=10)

class AccessCode(models.Model):
    code = models.CharField(max_length=10)
    state = models.CharField(max_length=10, default='OPEN')


views.py
def view(request, code):
    # for example code = 234567
    acccess_code = AccessCode.objects.select_for_update().filter(
    code=code, state='OPEN')

    acccess_code.delete()
    SaveAccessCode.objects.create(code=code)
    return

Concurrent request will generate two records of SaveAccessCode with same code, Please guide me how to handle this scenario in better way

Zaheer Jan
  • 233
  • 4
  • 13
  • Why wouldn't it serve the same data for both users? Why don't you want it to, and what should it do instead? And how are you calling this method? – Daniel Roseman Apr 27 '17 at 08:19

1 Answers1

0

You need to set some flag on the model when doing select_for_update, something like:

qs.first().update(is_locked=True)`

and before that should do select like

qs = self.select_for_update().filter(state='OPEN', is_locked=False).order_by('id')

Then after the user, I presume, has done something with it and saved, set the flag is_locked=False and save.

Also make the fetch_available_code as a @staticmethod.

Andrey Shipilov
  • 1,986
  • 12
  • 14