Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Is it possible to capture both original and new values in an on_change method?

Alejandro Santana
on 2/14/14, 3:59 PM 1,017 views

I would like to catch the original and new values from an onchange in a view. Is it possible? (any difference among v6.1, v7, trunk?)

This would be for: a) validating the new value and, if not right, revert to the original one. b) update another variable depending on these two values.


Grover Menacho

--Grover Menacho--
| 5 7 8
La Paz, Bolivia
--Grover Menacho--

Systems Engineer + MBA

Grover Menacho
On 2/14/14, 4:36 PM

Well it depends. If your record is already saved you can do it:

Let's see an example:

def onchange_shop_id(self, cr, uid, ids, shop_id, context=None):
        v = {}
        if shop_id:
            shop = self.pool.get('sale.shop').browse(cr, uid, shop_id, context=context)
            if shop.project_id.id:
                v['project_id'] = shop.project_id.id
            if shop.pricelist_id.id:
                v['pricelist_id'] = shop.pricelist_id.id
        return {'value': v}

Right here you have a code when you are changing shop_id on sales.

on shop_id you have the NEW VALUE

But you can get the old value doing this:

self.browse(cr, uid, ids[0]).shop_id.id

Why? Because ids it's going to store the id of the record on the database.

If you want to change first some value and without saving it, and then change it again, you can't get the old value.

So finally you can have the new value on your view, and the one that is stored on the database.

I hope this can help you.

Thanks a lot. As I see, I cannot do what I intended: change the value several times without saving, and having for each change the last and the new values (not the one saved in the table).

Anubía, soluciones en la nube, S.L., Alejandro Santana
on 2/18/14, 1:40 AM

I thought about this a bit. And you can do it with an additional auxiliary field (might be readonly and invisible). It should be handled by oneself inside the onchange method of the main field. For ejample if you have field my_field, you could create a my_field_prev and read it in the onchange_my_field. At that point, my_field_prev is the previous value of my_field. Then you do whatever you need and store in my_field_prev the final value of my_field. It needs duplicating values (and maybe taking care of unwanted onchange recursion). That's the solution I found. Not the optimum, but works.

Anubía, soluciones en la nube, S.L., Alejandro Santana
on 4/27/14, 10:22 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)


Asked: 2/14/14, 3:59 PM
Seen: 1017 times
Last updated: 3/16/15, 8:10 AM