Computed fields
Fields can have values calculated by a function, instead of simply reading a database stored value. A computed field is declared just like a regular field, but has the additional compute
argument defining the function used to calculate it.
In most cases computed fields involve writing some business logic, so we will develop this topic more in Chapter 7, ORM Application Logic - Supporting Business Processes. We will still explain them here, but will keep the business logic side as simple as possible.
Let's work on an example: Stages have a fold
field. We will add to To-do Tasks a computed field with the Folded? flag for the corresponding Stage.
We should edit the TodoTask
model in the todo_model.py
file to add the following:
# class TodoTask(models.Model): stage_fold = fields.Boolean( 'Stage Folded?', compute='_compute_stage_fold') @api.depends('stage_id.fold') def _compute_stage_fold(self): for task in self...