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 an additional argument compute
with the name of 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 can still explain them here, but keeping the business logic side as simple as possible.
Let's work on an example: stages have a fold
field. We will add to tasks a computed field with the Folded? flag for the corresponding stage.
We should edit the TodoTask
model in the todo_ui/todo_model.py
file to add the following:
# class TodoTask(models.Model): stage_fold = fields.Boolean( 'Stage Folded?', compute='_compute_stage_fold') @api.one @api.depends('stage_id.fold') def _compute_stage_fold(self): self.stage_fold = self.stage_id...