Declare a Django GeneratedField that the database keeps up to date
Answer: GeneratedField is always computed based on other fields in the model. The database manages and updates this field. The SQL syntax is GENERATED ALWAYS.
Stored and virtual columns
Generated columns are stored or virtual. A stored column is computed when it is written (inserted or updated) and occupies storage. A virtual column occupies no storage and is computed when it is read. GeneratedField.expression is an Expression the database uses to set the field value when the model is changed. Generated fields cannot reference other generated fields. db_persist selects the stored or virtual kind.
SQLite rules
Syntactically, generated columns are designated using a "GENERATED ALWAYS" column-constraint. If the trailing VIRTUAL or STORED keyword is omitted, then VIRTUAL is the default. Generated columns can be read, but their values can not be directly written. They can have a datatype and can participate in indexes, just like ordinary columns. Generated columns may not be used as part of the PRIMARY KEY. Every table must have at least one non-generated column. Only VIRTUAL columns can be added using ALTER TABLE.
| Django | SQLite | |---|---| | GeneratedField | Field computed from other fields in the model | | stored | Computed when written and occupies storage | | virtual | Occupies no storage and is computed when read | | GENERATED ALWAYS | SQL syntax for the column constraint |
See also
Claims
- id: C1 claim: GeneratedField is always computed from other fields and is updated by the database. source: S1 evidence: "A field that is always computed based on other fields in the model"
- id: C2 claim: It uses GENERATED ALWAYS and may be stored or virtual. source: S1 evidence: "Uses the GENERATED ALWAYS SQL syntax"
- id: C3 claim: SQLite generated columns can be read but their values cannot be written directly. source: S2 evidence: "Generated columns can be read, but their values can not be directly written"
Sources
- [S1] Model field reference. Django docs. https://docs.djangoproject.com/en/5.2/ref/models/fields/ . License: BSD-3-Clause. Accessed 2026-11-19.
- [S2] Generated Columns. SQLite. https://www.sqlite.org/gencol.html . License: Public-Domain. 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, Model field reference; SQLite, Generated Columns. BSD-3-Clause; Public-Domain. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Django docs, Model field reference; SQLite, Generated Columns. BSD-3-Clause; Public-Domain.
Cite this
iinventedlight. 2026. "Declare a Django GeneratedField that the database keeps up to date." iinventedlight Library, iil:note:2f6td773, version of 2026-11-19. https://iinventedlight.com/library/notes/django-generatedfield/. License: CC-BY-4.0.
@misc{iil_note_2f6td773,
author = {{iinventedlight}},
title = {{Declare a Django GeneratedField that the database keeps up to date}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/django-generatedfield/},
note = {Permanent ID iil:note:2f6td773. 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-generatedfield.table.json (table; CC-BY-4.0)
- django-generatedfield.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-generatedfield.md.