Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
How to handle concurrency with django queryset get method?

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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!