Welcome!

Share and discuss the best content and new marketing ideas, build your professional profile and become a better marketer together.

Sign up

You need to be registered to interact with the community.
This question has been flagged
1 Reply
47 Views

Hey Cyllo community! When's the best time to use `@api.depends_context`? I'm a bit confused on when it's really necessary. Any examples or explanations would be awesome!

Avatar
Discard

Here's how to use the '@api.depends_context' decorator in Odoo to make your computed fields aware of context values. This ensures your computed fields are recalculated when relevant context values change. 

For example,

class ProductTemplate(models.Model):
​ _inherit = 'product.template'

  ​discount_price = fields.Float(compute='_compute_discount_price') 

​@api.depends_context('discount_rate')
  ​def _compute_discount_price(self):
  ​for product in self:
  ​discount_rate = self.env.context.get('discount_rate', 0.0)
  ​product.discount_price = product.list_price * (1 - discount_rate / 100)

The 'depends_context' decorator specifies which context keys a non-stored "compute" method depends on. Each argument represents a key in the context dictionary.

Avatar
Discard

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!