Yes, in Odoo 16, a non-stored computed field can depend on another non-stored computed field for its value.
How it works:
- Odoo evaluates computed fields in the order of their dependencies.
- If
field_b depends on field_a (which is also computed and non-stored), Odoo will first compute field_a and then use its result to compute field_b.
- Both fields must be defined with the
@api.depends decorator to specify their dependencies.
Example:
from odoo import models, fields, api
class MyModel(models.Model):
_name = 'my.model'
base_field = fields.Integer()
computed_field_a = fields.Integer(compute='_compute_field_a', store=False)
computed_field_b = fields.Integer(compute='_compute_field_b', store=False)
@api.depends('base_field')
def _compute_field_a(self):
for record in self:
record.computed_field_a = record.base_field * 2
@api.depends('computed_field_a')
def _compute_field_b(self):
for record in self:
record.computed_field_b = record.computed_field_a + 10
- Here,
computed_field_b depends on computed_field_a, which itself depends on base_field.
- Odoo will automatically resolve the chain of dependencies and compute the fields in the correct order.
Key Points:
- No storage: Both fields are computed on-the-fly and not stored in the database.
- Performance: Chaining non-stored computed fields can impact performance, especially if the computation is complex or the chain is long.
- Circular dependencies: Avoid circular dependencies (e.g.,
field_a depends on field_b, which depends on field_a), as this will cause an error.
Would you like an example with a more complex use case or have a specific scenario in mind?