Enable Dark Mode!
By: Afra MP

How to Add Inverse Function for Compute Fields in Odoo 17

Technical Odoo 17

Odoo stands out as a robust open-source suite of business applications, providing developers with an extensive array of tools and functionalities to tailor and enrich the capabilities of their ERP systems. Among its notable features are calculated fields, enabling automated computation and presentation of values derived from other fields within the model. Nevertheless, Odoo does not inherently offer a built-in mechanism to define an inverse function for calculated fields. In this blog, we delve into the methodology for implementing an inverse function for a calculated array in Odoo 17.

Understanding Computed Fields

Before we dive into implementing the inverse function for computed fields in Odoo 17, let's first understand what computed fields are and how they work. A calculated field refers to a field that isn't directly stored in the database but rather computed dynamically based on the values of other fields or data. These fields enable the execution of calculations or manipulations on existing data within a model, presenting the derived outcome as if it were a standard field within the database

In Odoo, calculated fields are defined using the @api.depends decorator in the model, which indicates the dependencies of the calculated field. For example, consider a model that represents sales orders. We want to calculate the total amount based on the cost and quantity of each order line. We can define a calculated field named total_amount like this:

order_lines = fields.One2many('sale.order.line', 'order_id', string='Order Lines')
total_amount = fields.Float(string='Total Amount', compute='_compute_total_amount')
@api.depends('order_lines.price', 'order_lines.quantity')
def _compute_total_amount(self):
    for order in self:
        order.total_amount = sum(order.order_lines.mapped(lambda line: line.price * line.quantity))

In this instance, the calculated total_amount field relies on the price and quantity fields within the order_lines one2many field. Whenever alterations occur to these fields, the _compute_total_amount function is invoked to refresh the total_amount value.

The Need for an Inverse Function

While computed fields provide a powerful way to perform calculations based on other fields, they cannot be directly edited by the user. This limitation can be problematic in certain scenarios where you want to allow users to modify the value of a computed field.

To solve this problem, we can implement an inverse function for the calculated array. An inverse function is a method that is called when the value of a calculated field changes. The role of the inverse function is to ensure the values of the fields on which the calculated field depends are updated, thus maintaining the accuracy of the calculated field's returned value.

In the previous example, let's say we want to include an inverse function to update the quantity of each order line whenever the total_amount field changes. We can do this by adding an inverse argument to the total_amount field definition:

total_amount = fields.Float(string='Total Amount', compute='_compute_total_amount', inverse='_inverse_total_amount')

Now, whenever the total_amount field receives a new value, the _inverse_total_amount method will be invoked.

Implementing the Inverse Function

Now that we understand the need for an inverse function and how to define it, let's dive into the implementation details. In our example, the goal is to update the quantity of each order line whenever the total_amount field is changed. Here's how we can implement the _inverse_total_amount method:

def _inverse_total_amount(self):
    for order in self:
        if order.total_amount:
            average_price = order.total_amount / len(order.order_lines)
            for line in order.order_lines:
                line.quantity = average_price / line.price
            for line in order.order_lines:
                line.quantity = 0.0

In this updated example, the total_amount field now has an inverse='_inverse_total_amount' argument. This implies that each time the total_amount field is updated with a new value, the _inverse_total_amount method is triggered. Within this method, the average price is calculated by dividing the total amount by the number of order lines. Subsequently, the quantity of each order line is adjusted based on the new average price. Through the inclusion of this inverse function, the quantity of each order line dynamically adapts to maintain the updated total whenever changes are made to the total_amount field.


In this article, we explored how to implement an inverse function for compute fields in Odoo 17. By enabling the editing of computed fields through an inverse function, you can enhance the flexibility and usability of your Odoo applications. So go ahead, experiment with computed fields and inverse functions, and unlock the full potential of Odoo in your business processes.

If you need any assistance in odoo, we are online, please chat with us.


Leave a comment




Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635



Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.



Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message