9

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

Sid
  • 7,511
  • 2
  • 28
  • 41
  • Curious as to why the NOLOCK hint is needed in this scenario? Or is this just a nice-to-know kind of question? – Bryan Jan 26 '12 at 20:53
  • I simplified the query to ask the question, it is more complex. Nice to know as well :) – Sid Jan 26 '12 at 21:11
  • Fair enough :)...FWIW I think you'll have to resort to raw SQL – Bryan Jan 26 '12 at 21:42
  • Yes, I came up with the same conclusion. Was able to see some of the queries that Django was generating and they seem quite primitive. Using raw SQL now. – Sid Jan 26 '12 at 22:16

1 Answers1

1

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.

Andomar
  • 232,371
  • 49
  • 380
  • 404
  • 1
    Yeah I know I can do that or also use raw SQL but wanted to know if there's a way to use the Django Model Programming constructs and force a NOLOCK. Thanks anyway. – Sid Jan 26 '12 at 19:35