Django does not explicitly provide an API to do table locking. In my experience, well-designed code rarely requires locking the entire table, and most concurrency problems can be solved with row-level locking. This is the last effort: it does not solve concurrency, it just kills any attempt in concurrency.
If you really need a table-level lock, you can use the cursor and execute raw SQL statements:
from django.db import connection with connection.cursor() as cursor: cursor.execute("LOCK TABLES %s READ", [tablename]) try: ... finally: cursor.execute("UNLOCK TABLES;")
source share