Free support 120 Days
Free Support
Free support Mon - Fri From (10 AM - 10 PM) IST
Odoo>Understand the Difference between api-onchange and api-depends

Understand the Difference between api-onchange and api-depends

view data

Odoo

view data

5 MIN READ

view data

March 21, 2022

Loading

Method Decorators in Odoo

Odoo supports several decorators two among them are ‘onchange’ and ‘depends’ decorators. We are going to discuss the api-onchange and api-decorators. But before that, we will understand what decorators are and how to use them.

In this write-up, you will learn about the ‘Method Decorators’ in Odoo.

INTRODUCTION TO DECORATORS

What are decorators?

Decorators let you change how a method operates. Furthermore, it allows us to expand the behavior of another function. In other words, it accepts a function and adds specific functionality to it before returning the function.

Meaning, a decorator in Python is a function that takes another function as its argument and returns another function. Decorators can be extremely useful as they allow the extension of an existing function, without any modification to the original function source code.

Let’s understand with an example

Let our original division function calculate division but don’t check zero in the division so without changing the original division function we will check and prevent division if 0 exist in the argument.

def div_by_0(func):  ………………………. # custom function which will check the 0 in argument

def check(x, y):

if 0 in (x, y):

print(‘Cant perform Operation’)

return

return func(x, y)

return check

@div_by_0

def division(x, y):   ……………….   # original division function

result = x / y

print(result)

OR 

division = div_by_0(division)  …….. # to make this line shorter decorator uses

division(10,5)  ………………………….     #calling of the division method

@api.depends and @api.onchange both are decorators, but the main difference between them is stated further in the blog.

GETTING STARTED WITH ODOO DECORATORS

The decorators in Odoo Module includes the following functions:

API ONCHANGE DECORATOR

@api.onchange is triggered in the same view and same model when the fields specified inside the decorator value change. Also, onchange has instant function invokation when the field value is changed.

Syntax for api.onchange decorator in odoo

@api.onchange(‘field_name_1’, ‘field_name_2’, …)

def function(self):

#do something

return

It is an Odoo decorator, which runs when field_name value changes that is defined in the decorator, and value for field_name can be changed from its form view.

Let’s take this example

let partner_name is a many2one field from res.partner(contact), where res.partner has a gender field in its form view.

partner_name = fields.Many2one(“res.partner”,  string=”Name”)

partner_gender = field.char(“Gender”)

Code

@api.onchange(‘partner_name’)

def partner_gender(self):

for current_record in self:

if current_record.partner_name:

current_record.partner_gender = current_record.partner_name.gender

API DEPENDS DECORATOR

@api.depends a function defined with this decorator will be called if any change happens in the fields specified. These fields can be computed (instead of fetching data from the database). Computed fields are not stored by default, they are computed and returned when requested. Hence, depends uses the compute keyword to call the function for computing the value.

Syntax for api.depends decorator in odoo

@api.depends (‘field_name_1’, ‘field_name_2’, …)

def function(self):

#do something

return

Let’s discuss with an example

marks_obtained = fields.Float(‘Marks Obaintained’)

total_marks = fields.Float(‘Total Marks’)

percentage_score = fields.Float(string=‘Percentage’, store=True, compute=’_calculate_percent’)

Code

@api.depend(‘marks_obtained’, ‘total_marks’)

def _calculate_percent(self):

for record in self:

if  record.marks_obtained and record.total_marks:

record.percentage_score = (record.marks_obtained / record.total_marks)*100

What conclusion can be drawn?

Api. depends decorator will trigger if any of the fields specified in the decorator is changed and saved from form view. Whereas api.onchange decorator will trigger if any of the fields specified in the decorator is changed in the form before you save, it will trigger.

If you need any assistance with a deeper understanding of Odoo decorators, consult with Ksolves experts.

Odoo customization, integration, extension, and other services are available at our Ksolves store. Contact us for robust Odoo services at the given details:

Website: https://store.ksolves.com/
Email: sales@ksolves.com
Phone:+1(646)-203-1075, +91-7498170227

Loading

Need Help