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

Two on_change methods for the same field

By
Uillino
on 8/12/15, 6:09 AM 470 views

I have one field in my .py file and I've created two different function for it:

scode=  fields.Char('Code', size=3, required=True)

def uppercase(self, cr, uid, ids, scode, context=None):
if scode:
result = {'value': {'scode': str(scode).upper()}}
return result

def get_lenght(self, cr, uid, ids, scode, context=None):
if scode and len(scode) != 3:
My_error_Msg = 'Error'
raise osv.except_osv(("Warning!"), (My_error_Msg))
return

else:
return scode

def onchange_length_id(self, cr, uid, ids, scode, context=None):
res = {'value': {'scode': self.get_lenght(cr, uid, ids, scode, context=context)}}
return res

Now, my question is: how can I use both functions in my view?

If I had only one method, I would write:

<field name="scode" on_change="uppercase(scode)"/> 

Any idea?


Thanks!

Drees based his answer on merging the two methods in one. You can only use one 'on_change' method for a field.

Tarek Mohamed Ibrahim
on 8/12/15, 7:02 AM
1

Drees Far

--Drees Far--
1130
| 5 2 6
Tunis, Tunisia
--Drees Far--
Drees Far
On 8/12/15, 6:22 AM

Here is a solution friend:

Python:

scode=  fields.Char('Code', size=3, required=True)

def uppercase(self, cr, uid, ids, scode, context=None):
if scode:
result = {'value': {'scode': str(scode).upper()}}
return result

def get_lenght(self, cr, uid, ids, scode, context=None):
if scode and len(scode) != 3:
My_error_Msg = 'Error'
raise osv.except_osv(("Warning!"), (My_error_Msg))
return

else:
return scode

def onchange_length_id(self, cr, uid, ids, scode, context=None):
res = {'value': {'scode': self.get_lenght(cr, uid, ids, scode, context=context),
'scode': self.uppercase(cr, uid, ids, scode, context=context)
}}
return res

XML:

<field name="scode" on_change="onchange_length_id(scode)"/>

Hope this may help you :)

Regards.

Thank you for your reply. With your code, I get '{'value': {'scode_macro_attivita': str(scode_macro_attivita).upper()}}' when I put the value of scode. I set a 3 character value (eg. aaa) and when I fill the next field 'aaa' turn into '{'v' (the first 3 char of 'result').

Uillino
on 8/12/15, 9:54 AM

Solved! In the upper function I need to write 'return str(scode_macro_attivita).upper()'. Please Drees, modify your reply so I could give you a +1. Thank you for your help!

Uillino
on 8/12/15, 12:29 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

1 follower(s)

Stats

Asked: 8/12/15, 6:09 AM
Seen: 470 times
Last updated: 8/14/15, 3:00 PM