khaldi
khaldi

Reputation: 472

Sequentially execute Multiple R/W queries in same django views function

I have read and write queries in my single Django view function. As in below code:

def multi_query_function(request):
    model_data = MyModel.objects.all()  #first read command
    ...(do something)...
    new_data = MyModel(
                       id=1234,
                       first_property='random value',
                       second_property='another value'
                       )
    new_data.save()                     #second write command
    return render(request, index.html)

I need these queries in the function to be executed consecutively. For example, if multiple users use this function at the same time, it should execute this function for both users one by one. The 'read' of one user should only be allowed if the previous user has completed both of his/her 'read and write'. The queries of both users should never be intermingled.

Should I use table locking feature of my PostgreSQL DB or is there any other well managed way?

Upvotes: 1

Views: 335

Answers (1)

AKX
AKX

Reputation: 168834

Yep, using your database's locks are a good way to do this.

https://github.com/Xof/django-pglocks looks like a good library to give you a lock context manager.

Upvotes: 1

Related Questions