Odoo Help

Welcome!

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.

0

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

By
René Schuster
on 11/8/13, 8:21 AM 793 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}}
0
aharoen
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

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

3 follower(s)

Stats

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