This question has been flagged
1 Reply
7670 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.

Avatar
Discard
Best Answer

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.

Avatar
Discard
Author

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

Author

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.