Odoo : Choosing Between `related` and `compute` Attributes in Odoo Models

Jeevachaithanyan Sivanandan - Jan 8 - - Dev Community

In Odoo, when defining fields in a model, you often need to establish relationships between fields in the same model or related models. Two common attributes used for this purpose are related and compute. Let's explore when and how to use these attributes, and when not to use them based on your requirements.

related Attribute:

When to Use:

  • Direct Field Mapping:

    • Use related when the value of a field in your model can be directly fetched from another field, either within the same model or in a related model.
  • sales_person_id = fields.Many2one(related='res.users', string='Salesperson', store=True)

  • No Custom Computation:

    • Use related when there's no need for custom computation or logic to determine the value of the field. It provides a straightforward way to link fields.

Example:

`class ProjectTask(models.Model):
_name = 'project.task'

# Use related when sales_person_id can be directly fetched from res.users
sales_person_id = fields.Many2one(related='res.users', string='Salesperson', store=True)` 
Enter fullscreen mode Exit fullscreen mode

compute Attribute:

When to Use:

  • Custom Computation:

    • Use compute when the value of a field needs to be dynamically computed based on custom logic, other fields, or conditions.
  • sales_person_id = fields.Many2one('res.users', string='Salesperson', compute='_compute_sales_person_id', store=True)

  • Dependency on Other Fields:

    • Use compute when the value depends on other fields, and you want the field to be automatically recomputed when the dependent fields change.

Example:

`class ProjectTask(models.Model):
_name = 'project.task'

# Use compute when custom logic is needed to determine sales_person_id
Enter fullscreen mode Exit fullscreen mode

@api.depends('some_dependency_field')
def _compute_sales_person_id(self):
for task in self:
task.sales_person_id = some_function_to_determine_sales_person_id(task.some_dependency_field)
sales_person_id = fields.Many2one('res.users', string='Salesperson', compute='_compute_sales_person_id', store=True)`

When Not to Use:

  • Avoid Redundancy:
    • Avoid using both related and compute in the same field definition unless necessary. If you use related, it assumes direct field mapping, and adding a compute method might lead to redundant computations.

# Avoid this unless necessary
sales_person_id = fields.Many2one(related='res.users', string='Salesperson', compute='_compute_sales_person_id', store=True)

Conclusion:

Choosing between related and compute depends on the nature of the data and the logic involved. Use related for direct field mapping without custom computation and compute when dynamic calculations or custom logic are needed.

By understanding the distinctions between these attributes, you can make informed decisions when designing your Odoo models.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player