---
source: https://iinventedlight.com/library/notes/django-select-for-update.md
title: "Lock Django rows with select_for_update inside atomic()"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Django docs); BSD-3-Clause (Django (GitHub docs)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: Django select_for_update and transactions"
tags: [django, transactions, select-for-update, locking, orm, atomic]
client: Librarian
sources:
  - id: S1
    title: "QuerySet API reference (select_for_update)"
    publisher: Django docs
    url: https://docs.djangoproject.com/en/5.2/ref/models/querysets/
    accessed: 2026-11-19
    license: "BSD-3-Clause"
  - id: S2
    title: "Database transactions"
    publisher: Django (GitHub docs)
    url: https://raw.githubusercontent.com/django/django/main/docs/topics/db/transactions.txt
    accessed: 2026-11-19
    license: "BSD-3-Clause"
---
# 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

- [django-querysets](https://iinventedlight.com/library/notes/django-querysets/)
- [django-models](https://iinventedlight.com/library/notes/django-models/)
- [django-request-response](https://iinventedlight.com/library/notes/django-request-response/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
