-->

What is Main difference between @api.onchange and

2019-03-09 10:14发布

问题:

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.

回答1:

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



回答2:

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