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

时间:2015-04-07 18:09:17

标签: python orm openerp odoo openerp-8

我无法在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

2 个答案:

答案 0 :(得分:1)

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

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

并且最后只返回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

答案 1 :(得分:0)

  

由朋友回答

我应该编辑我的on_change函数以使用一个字典而不是两个字典返回数据 像这样:

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

感谢他