Enable Dark Mode!
how-to-create-computed-fields-in-odoo-19.jpg
By: Abhinraj R

How to Create Computed Fields in Odoo 19

Technical Odoo 19 Fields and widgets

Odoo is a powerful, flexible ERP system that allows developers to develop smart and automated business applications. Among various strong features, computed fields are used to generate field values dynamically based on other fields in a model.

The purpose of this blog is to walk through how to create computed fields in Odoo 19, and we will also highlight the key difference between the definition of a computed field using the @api.depends decorator and without using it, supported with practical examples for better understanding.

What Are Computed Fields?

In Odoo version 19, computed fields are considered special types of fields whose data is not recorded or stored in the database in a long-term capacity. Instead, the data is computed in real time using a Python method whenever a need arises. Computed fields can be very handy in situations where the data in a certain field depends on other data in other fields.

For example, a computed field might be used to automatically calculate the total price of a given product line by multiplying the quantity by the price per unit.

In addition, when defining a computed field, there is the use of the compute parameter, which refers to a function that defines the process for calculating the field’s value. By default, these fields are read-only, since they’re meant to display calculated results. However, with some extra configuration, such as defining an inverse function—they can also be made editable, giving developers more flexibility in handling business logic.

Defining a Computed Field in Odoo

To build a computed field in Odoo, you can follow these steps:

  • Declare the field in your model – Add it with the compute attribute, so Odoo knows the value will be generated automatically
  • Write the compute function – Create a method that defines how the field’s value should be calculated.
  • Link dependencies with @api.depends – Use this decorator to tell Odoo which fields should trigger recalculation when their values change.
  • Decide if it should be stored – If you want the calculated result to be saved in the database (useful for reporting and performance), set store=True.

Let’s say you want to calculate the total price of an item automatically whenever the quantity or unit price changes. You can achieve this by adding a computed field in your model:

from odoo import models, fields, api
class SaleOrderLine(models.Model):
   _inherit = 'sale.order.line'
   total_price = fields.Float(
       string="Total Price",
       compute="_compute_total_price",
       store=True
   )
   @api.depends('product_uom_qty', 'price_unit')
   def _compute_total_price(self):
       for line in self:
           line.total_price = line.product_uom_qty * line.price_unit

Explanation of the Example

  • total_price – This is the computed field that holds the calculated value.
  • store=True – Ensures that the computed result is saved in the database, making it available for searches, filters, and reports.
  • _compute_total_price method – Handles the calculation by multiplying the quantity with the unit price.
  • @api.depends – Watch the quantity and unit_price fields so that whenever either of them changes, the total_price is recalculated automatically.

Understanding the Role of @api.depends

The @api.depends decorator is used to declare the fields that a computed field relies on. Whenever any of these dependent fields change, Odoo automatically recalculates the computed field. If store=True is set, the updated value is saved in the database. This mechanism ensures that computed fields always reflect the latest data without manual updates.

How @api.depends Works

Using the previous example:

  • @api.depends('unit_price', 'quantity') signals Odoo to recompute total_price whenever either the unit_price or quantity changes.
  • When combined with store=True, the recalculated value is stored in the database, improving performance and avoiding unnecessary recalculations when the field is accessed.
  • This guarantees that total_price is always current, keeping your data accurate automatically.

What Happens Without @api.depends

If you don’t use the @api.depends decorator, Odoo won’t know which fields should trigger the computation of a computed field. As a result, the field will only be updated in limited situations, such as:

  • When a record is created or manually updated.
  • When the field is explicitly accessed, for example in a form view or a report.

Without @api.depends, the computed field can become outdated or incorrect, especially when its dependent fields change often. Moreover, if store=True is set, the stored value might not update properly, which can lead to inconsistencies in your data.

Example of a Computed Field Without @api.depends

from odoo import models, fields, api
class MyCustomModel(models.Model):
   _name = 'my.custom.model'
   _description = 'Custom Model for Compute Field Example'
   name = fields.Char(string="Description")
   unit_price = fields.Float(string="Unit Price")
   quantity = fields.Integer(string="Quantity")
   total_price = fields.Float(string="Total Price",   compute="_compute_total_price", store=True)
   def _compute_total_price(self):
       for record in self:
           record.total_price = record.unit_price * record.quantity

Understanding the Role of store=True

By default, computed fields in Odoo are not stored in the database (store=False) and are calculated dynamically each time they are accessed. Setting store=True tells Odoo to save the computed value in the database, which can improve performance and enable searching or filtering based on that field.

When to Use store=True

  • Use store=True for fields that are accessed frequently or involve resource-intensive calculations.
  • Use it when you need to search, filter, or group records based on the computed field.
  • Avoid using it for simple computations or rarely accessed fields, as storing them adds database overhead without much benefit.

By understanding how @api.depends and store=True work together, you can create computed fields in Odoo that are both efficient and reliable. Whether you’re implementing a basic calculation or a complex business rule, leveraging these features properly ensures your Odoo applications remain performant, accurate, and user-friendly.

Adding an Inverse Method to Computed Fields

Until now, we’ve talked about how the computed fields work and the importance of using `@api.depends` for correct computation. It is important to note that, by default, computed fields are read-only, but Odoo also provides an opportunity for editable computed fields using an inverse method.

In an inverse method, the user specifies how Odoo will update the dependent fields when the user changes the computed field value.

When Do You Need an Inverse Method?

You should use an inverse method when:

  • You want a user to edit a computed field directly in a user interface
  • The computed value will then update other fields accordingly.
  • Business logic allows reverse calculation

For instance, if total_price is editable, then modifying it should modify price_unit as well.

Example: Computed Field With Inverse Method

from odoo import models, fields, api
class SaleOrderLine(models.Model):
   _inherit = 'sale.order.line'
   total_price = fields.Float(
       string="Total Price",
       compute="_compute_total_price",
       inverse="_inverse_total_price",
       store=True
   )
   @api.depends('product_uom_qty', 'price_unit')
   def _compute_total_price(self):
       for line in self:
           line.total_price = line.product_uom_qty * line.price_unit
   def _inverse_total_price(self):
       for line in self:
           if line.product_uom_qty:
               line.price_unit = line.total_price / line.product_uom_qty

How This Works

  • Compute method calculates total_price
  • Inverse method recalculates price_unit when total_price is edited
  • Field becomes editable in form views
  • Data remains consistent both ways

Always ensure inverse logic is mathematically and functionally valid to avoid unexpected results.

Computed fields are an integral concept in Odoo 19, enabling the automation of computing, eliminating redundancy, and maintaining business logic at the model level. By correctly utilizing @api.depends, data is made sure to remain correct. In addition, computed fields are made searchable through the use of 'store=True.'

Similarly, the inverse method, when included, will improve flexibility with the ability to edit computed values, unlike when dealing with dependencies. Understanding compute, depends, and inverses means that a user will be able to efficiently and effectively develop useful Odoo applications.

To read more about How to Create Computed Fields in Odoo 18, refer to our blog How to Create Computed Fields in Odoo 18.


Frequently Asked Questions

Are computed fields stored in the database by default?

No. Computed fields are not stored unless store=True is explicitly set.

Can a computed field be editable?

Yes. You have to define an inverse method to make a computed field editable.

Is @api.depends mandatory?

While not syntactically mandatory, it is functionally essential—especially for stored computed fields to ensure automatic recalculation.

Can computed fields be searched or filtered?

Only if store=True is enabled. Non-stored computed fields cannot be used in domains.

Can a computed field depend on another computed field?

Yes, but ensure proper @api.depends chaining to avoid incorrect or missing recalculations.

Should every computed field be stored?

No. Use store=True only when needed for performance, reporting, or search operations.

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



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, KINFRA Techno Park
Kakkanchery, Calicut
Kerala, India - 673635

location

Kochi

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

location

Bangalore

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

Send Us A Message