How onchange() Function works @api.onchange()?

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.

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

Resume

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s