简体   繁体   中英

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

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?

What about writing

if field_1 and field_2 and field_3 and field_4:
    return{'value': {'field_5' : 'Completed' }}
else:
    return{'value': {'field_5' : 'Incomplete' }}

I also think that on_change method should be in field_5 xml definition:

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

This is because field_5 value deppends on the other four fields and not field_1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM