← All notes

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

Sources

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

Permanent ID: iil:note:2f6td773

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

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-generatedfield.md.