Odoo Help


How can I change the value of the invoking field of a on_change method?

René Schuster
on 11/8/13, 8:21 AM 1,193 views

If I use the same on_change method for multiple fields, how can I set the value of the field, that triggered the onchange method?

Eg: Two fields.integer 'a' and 'b' restricted to be below 9:

field name="a" on_change="onchange_intfield(a)" 
field name="b" on_change="onchange_intfield(b)"
def onchange_intfield(self, cr, uid, ids, number, context=None): 
   if number>8: 
   return {'value': {'....': 8}}
On 12/12/13, 1:10 AM

You can include context in your xml to difference field that triggering event

field name="a" on_change="onchange_intfield(a)"  context="{'somecode':'a'}"
field name="b" on_change="onchange_intfield(b)" context="{'somecode':'b'}"

And detect somecode value in your py def

def onchange_intfield(self, cr, uid, ids, number, context=None): 
         somecode = context.get('somecode',0)
         if somecode=='a':  
                 if number>8: 
                 return {'value': {'a': 8}}
         if somecode=='b':  
                 if number>8: 
                 return {'value': {'b': 8}}
          retun {}

Thank you, this might work!

René Schuster
on 12/12/13, 9:35 AM

About This Community

This platform 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.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

3 follower(s)


Asked: 11/8/13, 8:21 AM
Seen: 1193 times
Last updated: 3/16/15, 8:10 AM