on_change event in Odoo 8

Hi guys,

In this tutorial I will learn you how to create an on_change event that automatically updates a field when another field changes. In this sample I will create two new fields under sales > products in the tab ‘Procurements’. When a value changes on one of the two fields a third field will be updated with the total of the other two fields.

1. Creating new fields

The first step is to create new fields in the Python file. For example:

This adds two fields (CostPrice and ShippingCost) to the model product.template, which I first inherited. Both these fields are for numbers. After adding your new fields you need to add them to the view.

2. Add fields to view

In case you’re coding in the current module you can simply add the fields like this:

In case you have a new view you can simply add them but if you want to add them to an existing view you will need to inherit the record from the other module, just like in my sample. For example:

3. On_change function

Noticing anything in this code? I’ve already added the on_change event in the view like this:

This will search for the function on_change_price in your module and sends two values to the function, CostPrice and ShippingCost. This are the two fields that will contain the values and in these variables are now the values from the two fields. We’re almost there now, the only thing you need to do now is create the function on_change_price in your Python file. Return to your Python file and add the following code:

In the variables CostPrice and ShippingCost are the values that the user has entered. The variable total simply contains the sum of CostPrice and ShippingCost and this should now be filled in to our third field, which is named standard_price. After the value is calculated and added to the new field it should be returned so that the user can see it.
The result:
Products view - on_change fields

As you can see in this image the third field will be automatically calculated with the values from the field CostPrice and the field ShippingCost. The only thing you need to do for the on_change event to go off is to enter, tab or click anywhere else.

Want to see this module in action? You can download / view it on my Github.
If you want to you can also work with @api.onchange, which is specifically for Odoo 8.
Tutorial sponsored by Oocademy

11 thoughts on “on_change event in Odoo 8

  1. Federico says:

    Is it possible do the same thing with datetime fields?
    Look here: https://www.odoo.com/forum/help-1/question/v8-how-to-add-timestamp-to-a-processed-order-and-calculate-the-time-85758
    I’m trying to calculate the time (expressed in minutes) passed from the order creation till his delivery.
    I know how to do it on Python starting from a string but I didn’t figured out how to do it on Odoo using directly the fields. Do you have any clue?

  2. buket says:

    onchange function doesn’t work.please help me. my code :

    _columns = {
    ‘toplam’:fields.float(‘Toplam TL’),
    ‘kar_tutar_tl’:fields.float(‘kar tl’),

    def on_change_toplamkar(self, cr, uid, ids, kar_yuzde,toplam context=None):


    return res


    • Yenthe666 says:

      Hi buket,

      I don’t see a problem in the code that I see but a part is cut off too. Please provide more details about what goes wrong or what the error is, otherwise I can’t really help you.

Leave a Reply