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

Change the field value on change of other fields OpenERP/Python

By
supreeth
on 11/19/15, 7:02 AM 1,303 views

I have some 5 fields.

field_1: fields.char("Field") 
field_2: fields.char("Field")
field_3: fields.char("Field")
field_4: fields.char("Field")
field_5: fields.selection((('Incomplete','Incomplete'),('Completed','Completed')),'Field'),

By default field_5 will always be 'Incomplete':

_defaults = { 
'field_5': 'Incomplete',
}

My query is when all four fields has values, field_5 should automatically change to 'Completed' How to do this?

I had written an on_change function:

def on_change_module_code(self, cr, uid, ids, field_1,field_2,field_3,field_4): 
if field_1 :
return {'value': {'field_5': 'Completed'}}
if field_2 :
return {'value': {'field_5': 'Completed'}}
if field_3 :
return {'value': {'field_5': 'Completed'}}
if field_4 :
return {'value': {'field_5': 'Completed'}}

And in XML:

<field name="field_1" on_change="on_change_module_code(field_1,field_2,field_3,field_4)"/>;

Here when i enter first field, data is turning to 'Completed'. But it should turn to 'Completed' when all the fields has value.

How to do this?

3

Drees Far

--Drees Far--
1130
| 5 2 6
Tunis, Tunisia
--Drees Far--
Drees Far
On 11/19/15, 8:07 AM

Hello my friend;

here is the answer to your question:

Python:

def on_change_field(self, cr, uid, ids, field_1,field_2,field_3,field_4, context=None):

if field_1 and field_2 and field_3 and field_4:

return {'value': {'field_5': 'Completed'}}

 else:

return {'value': {'field_5': 'Inompleted'}}

XML: Call the onchange function in every field(field1, field2, field3, field4)

<field name="field_1" on_change="on_change_field(field_1,field_2,field_3,field_4)"/>

<field name="field_2" on_change="on_change_field(field_1,field_2,field_3,field_4)"/>

<field name="field_3" on_change="on_change_field(field_1,field_2,field_3,field_4)"/>

<field name="field_4" on_change="on_change_field(field_1,field_2,field_3,field_4)"/>

Best Regards.

plz my friend make me know if its Ok :)

Drees Far
on 11/19/15, 8:08 AM

@Drees Far. Thank you for the answer. It is working. But i am getting a error message for for all fields when i am trying to enter a message. Error Message is "TypeError: Cannot read property 'value' of null" What is this? I

supreeth
on 11/19/15, 11:10 PM

i have understood you because when you call the onchange from the first field the other fields are empty so you have to use the invisible attribute then call the onchange in the last field. if you didnt understand tell me im here for your help but plz vote ;)

Drees Far
on 11/20/15, 4:04 AM
1

Ahmed M.Elmubarak

--Ahmed M.Elmubarak--
2988
| 5 3 5
Sudan
--Ahmed M.Elmubarak--


Ahmed M.Elmubarak
On 11/19/15, 7:43 AM

Hello,

in the xml:


<field name="field_1" on_change="on_change_module_code(field_1,field_2,field_3,field_4)"/>
<field name="field_2" on_change="on_change_module_code(field_1,field_2,field_3,field_4)"/>
<field name="field_3" on_change="on_change_module_code(field_1,field_2,field_3,field_4)"/>
<field name="field_4" on_change="on_change_module_code(field_1,field_2,field_3,field_4)"/>


in .py:

def on_change_module_code(self, cr, uid, ids, field_1,field_2,field_3,field_4, context=None):
if field_1 and field_2 and field_3 and field_4:
  return {'value': {'field_5': 'Completed'}}
return {'value': {'field_5': Incomplete'}} 


I hope it'll help

Regards,

@Ahmed. Thank you for the answer. It is working. But i am getting a error message for for all fields when i am trying to enter a message. Error Message is "TypeError: Cannot read property 'value' of null" What is this? I

supreeth
on 11/19/15, 11:10 PM

I think you wrongly return the dict. kindly can you post your on_change method ...

Ahmed M.Elmubarak
on 11/21/15, 1:54 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

2 follower(s)

Stats

Asked: 11/19/15, 7:02 AM
Seen: 1303 times
Last updated: 3/6/16, 2:15 AM