Skip to Content
Menu
This question has been flagged
4 Replies
2485 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!

Avatar
Discard

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

Best Answer

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.

Avatar
Discard
Author

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

Author

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!

Related Posts Replies Views Activity
10
Jun 16
31779
1
Oct 15
8244
1
Mar 15
3729
5
Mar 15
10558
1
Mar 15
2836