What is the advantage of using Django 5.0's GeneratedField to have the database calculate a field value versus doing it in the Python save() method?
Last updated
A
GeneratedFieldmakes the database compute the value (as a stored or virtual column) on every insert/update, includingbulk_create(),update(), raw SQL and other apps.A
save()override is silently skipped by ORM paths that don't call it, so derived data can drift. The generated column is also a real column you can filter, order and index.Tradeoff: the expression may only use same-model fields and DB-supported functions (no complex Python logic). Choose
db_persist=True(stored) or virtual based on backend support.