0
votes

How to retrieve values on Many2one field using OnChange ?

the student should be registered in one standard and one group ... the standard have multi groups so i want that when i change the field standard .. the group field should be updated with the groups in that standard

When i try to do so it gives me an error

'Expected singleton: fci.standard.groups(3, 4, 5, 6)' 

I'm trying that when i change the standard field the group field will be updated to select only groups in this standard

Here is my fields

'standard_id': fields.many2one('fci.standard', string='Standard', required=True),
'group_id': fields.many2one('fci.standard.groups', string='Standard Group'),

Here is my function

def on_change_standard(self, cr, uid, ids, standard_id, context=None):
        val = {}
        if not standard_id:
            return {}
        student_obj = self.pool.get('fci.standard')
        student_data = student_obj.browse(cr, uid, standard_id, context=context)
        val.update({'group_id': student_data.groups_ids.id})
        return {'value': val}

and here is my xml

<field name="standard_id" on_change="on_change_standard(standard_id)" widget="selection"/>
<field name="group_id" widget="selection"/>
2
You can do this with domain. But you need to remove widget="selection". Dynamic domain cannot be used with widget="selection". Dynamic domains are simply ignored for selection widgets.no coder

2 Answers

3
votes

you can edit your code and add domain to 'group_id' field like this

<field name="standard_id" widget="selection"/>
<field name="group_id" widget="selection" domain[('standard_id','=',standard_id)]"/>

and edit your function on change like this

def on_change_standard(self, cr, uid, ids, standard_id, context=None):
        val = {}
        if not standard_id:
            return {}
        student_obj = self.pool.get('fci.standard')
        student_data = student_obj.browse(cr, uid, standard_id, context=context)
        val.update({'group_id': [ g.id for g in student_data.groups_ids ]})
        return {'value': val}

and it will work for you

bye

2
votes

No need to write on change method for that, you can achieve this by apply domain to that field. Try following,

<field name="standard_id" widget="selection"/>
<field name="group_id" widget="selection" domain="[('standard_id','=',standard_id)]"/>