Hello everyone. In this blog-post we will learn how onchange function works.
When we want to change a field’s value in a form (but hasn’t saved the form yet), onchange can be useful to automarically update other fields based on that value.
In this sample, we will create a new field ‘level’ under students model. When a value change on the field ‘note’ a second field ‘level’ will be updated.
Explanation of code
According to odoo documentation. @api.onchange “decorator will trigger the call to the decorated function if any of the fields specified in the decorator is changed in the form”.
In previous sample self corresponds to the record currently edited on the form. When in onchange context all work is done in the cache. So you can alter RecordSet inside your function without being worried about altering database. That’s the main difference with @api.depends.
Onchange function 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.