In practical business scenarios, there are fields whose values are calculated based on other fields,
either within the same records or in related records. To handle such situations,
computed fields in Odoo prove to be valuable. These fields derive their values through a specified
function.
The definition of computed fields in Odoo mirrors that of regular fields, with the exception of the
"compute" attribute, which designates the function responsible for the calculation. Computed fields
dynamically calculate values at runtime and are not inherently writable or searchable unless explicitly
configured.
Similar to regular fields, a computed field is declared, and it includes the "compute" attribute
specifying the function's name as a string or the function itself. This mechanism enables users to
efficiently calculate and manage values in Odoo based on predefined functions.
Dependencies:
Computed fields in Odoo typically rely on the values of other fields. To establish these dependencies,
the compute method incorporates the depends() decorator. This computation function dynamically
calculates values during runtime. To ensure efficiency and avoid unnecessary recalculations, it is
crucial to identify which fields depend on the
computed field. This information is added to the depends() decorator, specifying the fields that
influence the computed field's value.
For example
amount = fields.Float('Amount')
total = fields.Float('Total', compute="_compute_total")
@api.depends('amount')
def _compute_total(self):
for rec in self:
rec.total = 2.0 * rec.amount
Here we can calculate the total by using the function _compute_total. Computed fields are not stored in
the database by default. So we can use the store attribute to store the record in the database. We can
use the store = True flag.
For example
total = fields.Float('Total', compute="_compute_total", store=True)
Stored values of computed fields can be retrieved similarly to regular fields, without the need for
runtime recalculation. The ORM (Object-Relational Mapping) system determines when these stored values
should be recomputed and updated, ensuring efficient and timely handling of the computed field values.
Inverse Function
We know that by default the computed fields are read-only. Which means the user couldn’t set the value
for the computed fields. In some cases, it might be useful to still be able to set a value directly. For
that Odoo provides the ability to use an inverse function:
total = fields.Float(compute="_compute_total", inverse="_inverse_total")
amount = fields.Float("Amount")
@api.depends("amount")
def _compute_total(self):
for record in self:
record.total = 2.0 * record.amount
def _inverse_total(self):
for record in self:
record.amount = record.total / 2.0
The compute method is responsible for defining the field's value, whereas the inverse method
establishes the dependencies of the field. The inverse method is invoked during the record-saving
process, while the compute method is triggered whenever there is a change in its dependencies.