What is Main difference between @api.onchange and @api.depends in Odoo(openerp)?

bud-e picture bud-e · Feb 2, 2015 · Viewed 21k times · Source

In Odoo v8 there are many API decorators used. But i don't understand the main difference between @api.depends and @api.onchange.

Can anyone help me out from this one?

Thank You.

Answer

Hardik Patadia picture Hardik Patadia · Feb 2, 2015

@api.depends

This decorator is specifically used for "fields.function" in odoo. For a "field.function", you can calculate the value and store it in a field, where it may possible that the calculation depends on some other field(s) of same table or some other table, in that case you can use '@api.depends' to keep a 'watch' on a field of some table.

So, this will trigger the call to the decorated function if any of the fields in the decorator is 'altered by ORM or changed in the form'.

Let's say there is a table 'A' with fields "x,y & z" and table 'B' with fields "p", where 'p' is a field.function depending upon the field 'x' from table 'A', so if any of the change is made in the field 'x', it will trigger the decorated function for calculating the field 'p' in table 'B'.

Make sure table "A" and "B" are related in some way.

@api.onchange

This decorator will trigger the call to the decorated function if any of the fields specified in the decorator is changed in the form. Here scope is limited to the same screen / model.

Let's say on form we have fields "DOB" and "Age", so we can have @api.onchange decorator for "DOB", where as soon as you change the value of "DOB", you can calculate the "age" field.

You may field similarities in @api.depends and @api.onchange, but the some differences are that scope of onchange is limited to the same screen / model while @api.depends works other related screen / model also.

For more info, Here is the link that describe all API of Odoo v8.