When and where to use the computed fields and related fields & uses

Odoo is a highly flexible ERP system that allows developers to extend and customize models through different types of fields. Among them, computed fields and related fields are two powerful options that provide dynamic behavior and reduce redundancy in data management.

In this blog, we’ll explore:

  • What computed and related fields are.

  • The differences between them.

  • When and where to use each one.

  • Practical examples to help you decide the right approach.

Computed Fields

A computed field in Odoo is a special type of field whose value is not manually entered by the user but is automatically calculated based on a defined logic. Unlike regular fields, which store data directly in the database, computed fields can either exist temporarily in memory or be stored in the database if the store=True parameter is set. The calculation is performed using a Python method decorated with @api.depends, which specifies the fields that trigger a recalculation whenever their values change.

Here is the example code to add computed field


from odoo import models, fields, api

class SaleOrder(models.Model):

    _inherit = "sale.order"


    total_discount = fields.Float(

        string="Total Discount",

        compute="_compute_total_discount",

        store=True

    )


    @api.depends('order_line.discount')

    def _compute_total_discount(self):

        for record in self:

record.total_discount = sum(record.order_line.mapped('discount'))

When to Use Computed Fields:

  • When the field’s value depends on a calculation or logic.
  • When you don’t want to store redundant data, but calculate it on demand.
  • When values need to update automatically based on other fields.

Use Cases:

  • Tax or discount calculations.
  • KPI metrics (e.g., sales performance, stock availability).
  • Derived values such as total order amount or average ratings.

Related Fields

A related field in Odoo is a special type of field that provides a direct reference to a field in another, related model. Instead of duplicating data or writing custom logic to fetch information from another model, a related field allows you to access that value directly through a chain of relationships. This makes development faster, reduces code complexity, and ensures consistency across your application.


from odoo import models, fields


class SaleOrderLine(models.Model):

    _inherit = "sale.order.line"


    customer_email = fields.Char(

        related="order_id.partner_id.email",

        store=True,

        readonly=False

    )



Here, customer_email is directly taken from the customer linked to the sales order.


When to Use Related Fields:

  • When you just want to show or use an existing field from another model.
  • When no complex logic is required.
  • To avoid duplicating fields in multiple models.

Use Cases:

  • Display customer details (name, email, phone) in order lines.
  • Show product category directly on invoice lines.
  • Fetch manager name from employee records into another model.

Best Practices

  • Use computed fields when business logic is involved.
  • Use related fields for simple lookups without repeating code.
  • Add store=True if the value is needed in searches, filters, or reporting.
  • Avoid overusing computed fields with heavy logic in large datasets (may affect performance).

Computed vs Related Fields

Aspect

Computed Fields

Related Fields




Definition

Value is calculated using Python logic.

Value is directly pulled from another model’s field.

Logic

Requires a method (@api.depends).

No method required, just reference path.

Use Case

Complex calculations, aggregations, or derived data.

Simple reference to existing fields.

Performance

May be heavy if logic is complex and not stored.

Lightweight and efficient.

Storage

Optional (store=True to save in DB).

Can be stored if needed (store=True).

Both computed fields and related fields are essential tools for Odoo customization.

  • Computed fields are ideal when calculations or logic are needed.
  • Related fields are best when you simply need to fetch existing data from another model.

By choosing the right field type, you can keep your Odoo modules efficient, maintainable, and user-friendly.

Share this post
Tags
Archive
How to Inherit and Extend Modules in Odoo