Odoo 19 continues to be one of the most flexible ERP platforms, giving developers and businesses the ability to customize workflows efficiently. One of the common tools used in customization is the computed field, which automatically derives its value from other fields.
While computed fields are powerful, they are read-only by default. This means that users can see the calculated value but cannot edit it directly. In situations where you want the field to be editable and for changes to update related fields automatically, you need to add an inverse function.
This article explains how inverse functions work in Odoo 19, why they are important, and demonstrates how to implement them with an example.
What are Computed Fields?
A computed field in Odoo is a field whose value is not stored directly but calculated from other fields using a Python method. These fields are especially useful when a value depends on logical calculations or relationships with other records.
Defining a Computed Field
You can create a computed field in Odoo by using the compute attribute when defining the field. Here’s a simple example:
class ExampleModel(models.Model):
_name = 'example.model'
total = fields.Float(compute='_compute_total')
@api.depends('line_ids.price', 'line_ids.quantity')
def _compute_total(self):
for record in self:
record.total = sum(line.price * line.quantity for line in record.line_ids)
Here:
- The field total is calculated using the _compute_total method.
- The decorator @api.depends ensures that the total is recalculated whenever price or quantity changes in related line_ids.
Limitations of Computed Fields
By default, computed fields in Odoo 19 cannot be edited manually. This is intentional since their value is derived automatically. However, in many real business cases, you may want the user to adjust a computed field, and then have Odoo recalculate related values accordingly.
For example:
- If total is automatically calculated from line quantities and prices, you may still want the user to update total manually and push those changes back to the prices or quantities.
This is where the inverse function comes in.
What is an Inverse Function?
An inverse function is a method that defines how Odoo should update other fields when a computed field is changed manually by a user.In short:
- Compute method: how to calculate the field’s value automatically.
- Inverse method: how to update dependent fields if the computed field is edited by the user.
This makes computed fields two-way: Odoo can calculate them, and also react when they are modified.
Adding an Inverse Function
Here’s the structure of a computed field with an inverse function:
class ExampleModel(models.Model):
_name = 'example.model'
total = fields.Float(compute='_compute_total',
inverse='_inverse_total')
@api.depends('line_ids.price', 'line_ids.quantity')
def _compute_total(self):
for record in self:
record.total = sum(line.price * line.quantity for line in
record.line_ids)
def _inverse_total(self):
for record in self:
# Logic to update the dependent fields when total is
# manually edited
total_quantity = sum(line.quantity for line in
record.line_ids)
if total_quantity:
for line in record.line_ids:
line.price = record.total / total_quantity
Explanation:
- The total field is calculated automatically using _compute_total.
- When a user changes the total manually in the UI, the _inverse_total method is triggered.
- In this example, the inverse method redistributes the new total back across the line_ids by updating their price.
Components of the Inverse Function
- compute='_compute_total': Specifies the method that calculates the field’s value automatically based on other fields.
- inverse='_inverse_total': Defines the method that updates related fields whenever the computed field is edited manually.
Using an inverse method ensures that data remains consistent by propagating changes made to the computed field back to its dependent fields.
Conclusion
Computed fields in Odoo 19 make it easy to automate dynamic calculations. However, in cases where you need the flexibility to edit these fields manually, adding an inverse function is the solution.
By pairing a compute method with an inverse method, you can make computed fields editable and responsive, ensuring that changes flow back to the related data logically.
This approach improves both the functionality and usability of your custom Odoo models.
To read more about How to Add Inverse Function for Compute Fields in Odoo 18, refer to our blog How to Add Inverse Function for Compute Fields in Odoo 18