为什么on_change不能同时刷新2个字段?

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

我无法通过 on_change 中的一个字段更改 2 个字段,它只会更改 group_idattendance_line 永远不会更改

这是我的 on_change 函数

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

我尝试使用它它只会改变 group_id 而 attendance_line 永远不会改变 这是我的字段

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

这是我的 xml 代码:

<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>

我应该提到 group_id 可以将 attendance_line 字段更改为并且它工作得很好,这是我的 on_change 代码:

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

Answered by a friend

我应该用一个字典而不是两个字典将 on_change 函数编辑为 return 数据 像这样:

def onchange_standard_id(self, cr, uid, ids, standard_id,context = None):
        res={}
        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}})
        res.update({'group_id': [ g.id for g in student_data.groups_ids]})
        return res

感谢他

您可以处理模型中可用的任何字段,请尝试以下代码模式。

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

最后 return 只有 res.

根据您的代码,

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