Lock Django rows with select_for_update inside atomic()
Answer: Call select_for_update() in a transaction.atomic() block when two views might update the same rows. On supported databases this generates a SELECT ... FOR UPDATE SQL statement, and the rows stay locked until the end of the transaction. Django's default behavior is to run in autocommit mode, so each query is immediately committed unless a transaction is active. The lock lasts until the end of the transaction block.
Open a transaction first
Django uses transactions or savepoints automatically for ORM operations that require multiple queries, especially delete() and update(). Use with transaction.atomic(): for the lock and the writes. You can also set ATOMIC_REQUESTS to True. Before calling a view function, Django starts a transaction, then commits it if the response is produced without problems, or rolls back if the view produces an exception.
How the lock behaves
Evaluating the queryset (the for entry in entries pattern in the docs) locks every matched entry until that transaction block ends. Other transactions will be prevented from changing or acquiring locks on them. If a lock is already acquired, the query will block until the lock is released. Call select_for_update(nowait=True) to make the call non-blocking.
| nowait=True | skip_locked=True | |---|---| | nowait=True | The call is non-blocking. If a conflicting lock is already acquired, DatabaseError will be raised when the queryset is evaluated. | | skip_locked=True | Ignore locked rows. nowait and skip_locked are mutually exclusive and attempts to enable both raise ValueError. | | of=(...) | By default, rows of related objects specified in select_related() are locked too. Name the relations to lock; use "self" for the queryset's model. |
Gotcha
nowait and skip_locked are mutually exclusive. skip_locked and no_key require a supported database.
See also
Claims
- id: C1 claim: select_for_update locks matched rows until the transaction ends. source: S1 evidence: "Returns a queryset that will lock rows until the end of the transaction"
- id: C3 claim: Django runs in autocommit unless a transaction is already active. source: S2 evidence: "Django's default behavior is to run in autocommit mode."
- id: C4 claim: If the view produces an exception, Django rolls back. source: S2 evidence: "If the view produces an exception, Django rolls back the"
Sources
- [S1] QuerySet API reference (select_for_update). Django docs. https://docs.djangoproject.com/en/5.2/ref/models/querysets/ . License: BSD-3-Clause. Accessed 2026-11-19.
- [S2] Database transactions. Django (GitHub docs). https://raw.githubusercontent.com/django/django/main/docs/topics/db/transactions.txt . License: BSD-3-Clause. Accessed 2026-11-19.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Django docs, QuerySet API reference (select_for_update); Django (GitHub docs), Database transactions. BSD-3-Clause; BSD-3-Clause. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Django docs, QuerySet API reference (select_for_update); Django (GitHub docs), Database transactions. BSD-3-Clause; BSD-3-Clause.
Cite this
iinventedlight. 2026. "Lock Django rows with select_for_update inside atomic()." iinventedlight Library, iil:note:nmtxy6kz, version of 2026-11-19. https://iinventedlight.com/library/notes/django-select-for-update/. License: CC-BY-4.0.
@misc{iil_note_nmtxy6kz,
author = {{iinventedlight}},
title = {{Lock Django rows with select\_for\_update inside atomic()}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/django-select-for-update/},
note = {Permanent ID iil:note:nmtxy6kz. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- django-select-for-update.table.json (table; CC-BY-4.0)
- django-select-for-update.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/django-select-for-update.md.