Search code examples
pythonormodooopenerp-8

Why can't on_change refresh 2 fields is the same time?


i can't change 2 fields by one field in on_change , it only changes the group_id and the attendance_line never change

here is my on_change function

def onchange_standard_id(self, cr, uid, ids, standard_id,context = None):
        res={}
        val = {}
        student_list = []
        stud_obj = self.pool.get('fci.student')
        student_obj = self.pool.get('fci.standard')
        stud_id = stud_obj.search(cr, uid,[('standard_id', '=', standard_id)])
        student_data = student_obj.browse(cr, uid, standard_id, context=context)
        for id in stud_id:
            student_dict = {'student_id':id}
            student_list.append(student_dict)
        res.update({'value': {'attendance_line': student_list}})
        val.update({'group_id': [ g.id for g in student_data.groups_ids]})
        return res,val

i try to use it it only change the group_id and the attendance_line never change here is my fields

'group_id': fields.many2one('fci.standard.groups', string='Group'),
'attendance_line': fields.one2many('fci.attendance.line', 'attendance_id', string='Attendance Line', required=True), 

and here is my xml code :

<field name="standard_id" on_change="onchange_standard_id(standard_id)"
                                       widget="selection"/>
                                <field name="group_id" attrs="{'invisible':[('lab_section_sheet','not in',['lab_sheet','section_sheet'])]}"  widget="selection" domain="[('standard_id','=',standard_id)]" on_change="onchange_group_id(group_id)"/>

<field name="attendance_line" colspan="4" nolabel="1" domain="[('standard_id','=',standard_id)]">
                                    <tree string="Attendance Line" editable="top">
                                        <field name="student_id"/>
                                        <field name="present"/>
                                    </tree>
                                </field>

and i should mention that group_id can change the attendance_line field to and it work perfectly and here is my on_change code :

def onchange_group_id(self, cr, uid, ids, group_id,context = None):
        res={}
        student_list = []
        stud_obj = self.pool.get('fci.student')
        stud_id = stud_obj.search(cr, uid,[('group_id', '=', group_id)])
        for id in stud_id:
            student_dict = {'student_id':id}
            student_list.append(student_dict)
        res.update({'value': {'attendance_line': student_list}})
        return res

Solution

  • You can deal with any fields available in the model, try following code pattern.

    result = {'value': {}}
    result['value']['field1'] = value1
    result['value']['field2'] = value2
    result['value']['field3'] = value3
    

    and at the end return only res.

    According to your code,

    def onchange_standard_id(self, cr, uid, ids, standard_id,context = None):
            result = {'value': {}}
            student_list = []
            stud_obj = self.pool.get('fci.student')
            student_obj = self.pool.get('fci.standard')
            stud_id = stud_obj.search(cr, uid,[('standard_id', '=', standard_id)])
            student_data = student_obj.browse(cr, uid, standard_id, context=context)
            for id in stud_id:
                student_dict = {'student_id':id}
                student_list.append(student_dict)
            result['value']['attendance_line'] = student_list
            result['value']['group_id'] = [ g.id for g in student_data.groups_ids]
            return res