Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ensure a SQL like NOLOCK when using Django Model Queries

Tags:

sql

django

pyodbc

I have a Django Model "Users" tied to an existing MS SQL Server database table. I am reading the table thus:

Users.objects.filter(userid='xyz').filter(status='active')

I want to know what locking constructs would this translate to, as in, would this sort of a read Lock the table? In SQL I would have done:

SELECT * from users (nolock) where userid='xyz' and status='active'

Is there a way to explicitly specify a "nolock" via Django Model queries?

Searched a lot in the Django as well as django-pyodbc documentation without any success.

Thanks.

p.s.: Using django-pyodbc and pyodbc drivers

like image 957
Sid Avatar asked Jan 26 '12 16:01

Sid


1 Answers

You can create a view:

create view dbo.vw_Users
as
select  col1
,       col2
,       ...
from    dbo.Users with (nolock)

And have Django read from the view instead of the table.

like image 178
Andomar Avatar answered Nov 09 '22 02:11

Andomar