Triggering Computed Method On Change Of Context Keys

Using @api.depends_context()

by Sajjad Hussain

In Odoo Python development, the @api.depends_context() decorator is used to specify that a computed field or a method depends on specific context keys (values) provided during recordset operations. This decorator is part of the Odoo framework's API and is used to trigger the recomputation of fields or methods when relevant context keys change.

Purpose of @api.depends_context()

When you define a computed field or a method in an Odoo model that depends on certain context values (passed to the recordset), you can use @api.depends_context() to declare these dependencies explicitly. This ensures that whenever the specified context keys change, the computed field or method is recomputed appropriately.

Usage Example

Here is an example of using @api.depends_context() with a computed field in an Odoo model:

python
from odoo import models, fields, api

class SaleOrder(models.Model):
  _inherit = 'sale.order'

  # Computed field that depends on the 'force_company' key in the context
  @api.depends_context('force_company')
  def _compute_company_name(self):
        for order in self:
           company_id = order.env.context.get('force_company')
           if company_id:
              company = order.env['res.company'].browse(company_id)
              order.company_name = company.name
           else:
              order.company_name = ''

# Computed field to store company name based on context company_name = fields.Char(
      string='Company Name',
 
      compute='_compute_company_name',
      store=True
)

In this example:

  • We have a model SaleOrder that inherits from sale.order.
  • We define a computed field company_name which is computed based on the value of the 'force_company' key in the context.
  • The @api.depends_context('force_company') decorator specifies that the _compute_company_name() method depends on the 'force_company' key in the context.
  • Inside the computation method _compute_company_name(), we use self.env.context.get('force_company') to retrieve the value of the 'force_company' key from the context.
  • If the 'force_company' key is present in the context, we retrieve the corresponding company name using order.env['res.company'].browse(company_id).
  • The computed company_name field is then updated based on the retrieved company name.

You should use @api.depends_context() when:

  • You want to compute a field or method based on specific context keys provided during recordset operations.
  • The computation result depends on the values of context keys, and you want Odoo to automatically trigger recomputation when these context keys change.

By using @api.depends_context(), you can ensure that your computed fields and methods in Odoo models are reactive to changes in relevant context keys, providing dynamic and context- aware behavior within your Odoo applications. This helps in building flexible and responsive business logic that adapts to different context scenarios.

in ERP
Solving "Assets Not Found" Error in Odoo Database UI