What is onchange method ?

This method automatically updates other fields based on the value of the field that you are updating, and all that happens on the fly without the need to save the form. At the end, if you want to store the changes permanently you must press the save button.

An onchange method is a method decorated with the decorator onchange, which as input arguments receives a name of the fields on which update the method should be called.

For example, if you want every time when a product is changed in the invoice line to be set automatically price for it,  you can do that simply by creating onchange method for the product_id field in account.invoice.line model:

from openerp import models, api

class InvoiceLine(models.Model):
    _name = 'account.invoice.line'

    @api.onchange('product_id')
    def onchange_product_id(self):
        self.price_unit = self.product_id.lst_price

Now every time when the product is changed, price value will be changed also.

Leave a Reply

Your email address will not be published.