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

Onchange function on field.selection

By
fdim
on 7/25/13, 2:36 PM 4,667 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"/>
1
Nishant Kashyap
On 7/25/13, 4:08 PM

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.

Thanks Nishant

fdim
on 7/26/13, 10:41 AM

My Pleasure!

Nishant Kashyap
on 7/26/13, 11:21 AM

Nic,1 vote

aci aisha
on 2/9/15, 2:24 AM
1

Grover Menacho

--Grover Menacho--
2523
| 5 7 8
La Paz, Bolivia
--Grover Menacho--

Systems Engineer + MBA

Grover Menacho
On 7/25/13, 6:31 PM

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

Thanks Grover..it works now

fdim
on 7/26/13, 10:41 AM

Nic,1 tick & 1 vote

aci aisha
on 2/9/15, 2:23 AM
0
ahmed
On 12/9/15, 5:07 AM

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

0
Mazhar Iqbal Rana
On 2/9/15, 2:20 AM

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

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

0 follower(s)

Stats

Asked: 7/25/13, 2:36 PM
Seen: 4667 times
Last updated: 12/9/15, 5:07 AM