31
votes

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.

3

3 Answers

52
votes

@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.

5
votes

@api.onchange works in virtual records assignment on these records is not written to the database, just used to know which value to send back to the client.

Fields can be computed (instead of read from the database) using the compute parameter, it must assign the computed value to the field, it uses the values of other fields from the same model or others model (unlike @api.onchange which work only with the fields in the same view), it should specify fields using api.depends().

For more information. Please check out our blog : https://odooforbeginnersblog.wordpress.com/2017/03/01/how-to-override-an-api-depends-decorated-method/

2
votes

@api.depends

The function defined with this decorator will be called if any change happens in the fields specified. Moreover, the change to the field can be from ORM or changes in the form. Furthermore, if a compute function value depends on another field, then it must be specified using depends.

@api.onchange

The function of this decorator will be called when the field value changes. Moreover, it supports only single field names; on the contrary, dotted names such as parent_id.field_name will not be considered. Furthermore, onchange methods are invoked on pseudo-records that contain values of the form. The following is an example of the same:

Reference for better understanding with example : https://www.cybrosys.com/blog/method-decorators-odoo-13