Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

odoo

odoo-8

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.

like image 309
bud-e Avatar asked Feb 02 '15 03:02

bud-e


People also ask

What is the difference between API depends and API Onchange?

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.

What is the difference between Onchange and compute Odoo?

Onchange is triggered only when one of the given attributes changes its value. 3. Every compute method should assign a value to the computed field therefore, it is necessary to ensure that the compute method will assign a value to the field on every condition.

What is API Onchange in Odoo?

--%> Odoo api.onchange decorator is used to invoke a method when a field's value changed. It can be used to validate the data or to trigger an action. A detailed explanation of the technical and functional aspects of the Onchange mechanical of Odoo.

What is API in Odoo?

The ORM API, allows you to write complex logic and wizards to provide a rich user interaction for your apps. The ORM provides few methods to programmatically interact with the Odoo data model and the data, called the Application Programming Interface (API).


2 Answers

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

like image 170
Hardik Patadia Avatar answered Sep 19 '22 09:09

Hardik Patadia


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

like image 25
Odoo For Beginners Avatar answered Sep 18 '22 09:09

Odoo For Beginners