Skip to Content
Menu
This question has been flagged
9 Replies
18212 Views

I try to add an onchage function on a field.selection, but it doenst work, here the code:

class my_class(osv.osv):

    _name = "my.class"

    _columns = {
        'field1': fields.selection([('1', 'A'), ('1', 'B')], 'my filed 1'),
        'field2': fields.boolean('My field 2'),
    }

    def onchange_field1(self, cr, uid, ids, field1, field2, context=None):
        if field1=='A':
            v = {'field2': 1}
            return {'value': v}
        return {}
res_partner()

in xml file :

 <field name="field1" on_change="onchange_field1(field1, field2)"/>
<field name="field2"/>
Avatar
Discard
Best Answer

Your are checking field1 with the label 'A' check it with the value '1'. Change your code to:

if field1=='1':

Now it will work.

Avatar
Discard
Author

Thanks Nishant

My Pleasure!

Nic,1 vote

Best Answer

It's not neccesary to send the field2, you can do this:

class my_class(osv.osv):

_name = "my.class"

_columns = {
    'field1': fields.selection([('1', 'A'), ('1', 'B')], 'my filed 1'),
    'field2': fields.boolean('My field 2'),
}

def onchange_field1(self, cr, uid, ids, field1):
    if field1=='1':
        v = {'field2': True}
        return {'value': v}
    return {}

res_partner()

in xml file :

<field name="field1" on_change="onchange_field1(field1)"/>
<field name="field2"/>

As you can see you have to compare field1 with the key not with the name

Avatar
Discard
Author

Thanks Grover..it works now

Nic,1 tick & 1 vote

Best Answer

Hello,

I need to change my slection elements after change field1 .

this code doesn't work , who has an idea how to fix it ?

_columns = {

'field1': fields.selection([('1', 'A'), ('1', 'B')], 'my filed 1'),

'field2': fields.selection([('aa', 'C'), ('bb', 'D')], 'my filed 2'),

}

def onchange_field2(self, cr, uid, ids,field1,context=None):

    lst= [('5', '5'), ('6', '6'), ('7', '7'), ('8', '8')]

    try:

        lst.remove([item for item in lst if item[0] == '5'][0])

    except IndexError as e:

        pass

    v = {'field2': lst}

    return {'value': v}

thank you

Avatar
Discard
Best Answer

What if I have to change string of boolean field instead of value?How will I then achieve?

Avatar
Discard