← All notes

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

Sources

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

Permanent ID: iil:note:nmtxy6kz

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.},
}

Markdown

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.

All artifact files (JSON index)

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.