Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django and Sqlite Concurrency issue

I've done a bit of reading related to the concurrency issues with sqlite, but I don't see how they'd apply to Django since it's inherently single threaded. I'm not using any multiprocess modules either. I have absolutely no experience with concurrent programming either, so if someone can identify WHY the following code is causing an OperationalError: 'database is locked' I'd be grateful.

views.py

def screening(request, ovramt=None):
errors = []
if request.method == "POST":
    form = ScreeningForm(request.POST)
    if form.is_valid():
       print "Woo valid!!"
    return HttpResponse()

else: # GET            
    if ovramt is None:
        o = Ovramt.objects.select_related(depth=1).latest("date_completed")
        print "found?"
        print o.id
    else:
        try:
            o = Ovramt.objects.select_related(depth=1).get(id=ovramt)
        except:
            errors.append("OVRAMT NOT FOUND") 


    if o.residents.count() <= 0:
        o.add_active_residents()
    residents = list(o.residents)

models.py

def add_active_residents(self):
    ssa_res = SSA_Resident.objects.select_related(depth=1).filter(ssa=self.ssa, active=True)
    for r in ssa_res:
        self.residents.add(r.resident) # Fails Here
    self.save()

The add_active_residents method works fine, until it is called from the views module. Is there an open connection to the database open in the view which prevents writing from the model? Does someone have an explanation why this code will error?

like image 250
Josh Smeaton Avatar asked Jan 23 '23 20:01

Josh Smeaton


1 Answers

In the following method function

def add_active_residents(self):
    ssa_res = SSA_Resident.objects.select_related(depth=1).filter(ssa=self.ssa, active=True)
    for r in ssa_res:
        self.residents.add(r.resident) # Fails Here
    self.save()

Why is there a select_related? You only really need the FK's of ssa_res items. Why do additional queries for related items?

like image 126
S.Lott Avatar answered Jan 30 '23 09:01

S.Lott