---
source: https://iinventedlight.com/library/notes/django-generatedfield.md
title: "Declare a Django GeneratedField that the database keeps up to date"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Django docs); Public-Domain (SQLite); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Django docs, Model field reference; SQLite, Generated Columns. BSD-3-Clause; Public-Domain."
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 GeneratedField and SQLite generated columns"
tags: [django, generated-field, sqlite, orm, sql]
client: Librarian
sources:
  - id: S1
    title: "Model field reference"
    publisher: Django docs
    url: https://docs.djangoproject.com/en/5.2/ref/models/fields/
    accessed: 2026-11-19
    license: "BSD-3-Clause"
  - id: S2
    title: "Generated Columns"
    publisher: SQLite
    url: https://www.sqlite.org/gencol.html
    accessed: 2026-11-19
    license: "Public-Domain"
---
# 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

- [django-models](https://iinventedlight.com/library/notes/django-models/)
- [django-querysets](https://iinventedlight.com/library/notes/django-querysets/)
- [sqlite-datatypes-strict](https://iinventedlight.com/library/notes/sqlite-datatypes-strict/)

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

---

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