如何检索Many2one字段中的值作为选择字段? [英] How to retrive values in Many2one field as selection field?

查看:185
本文介绍了如何检索Many2one字段中的值作为选择字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用OnChange检索Many2one字段上的值?

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

这是我的领域

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

这是我的职责

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}

这是我的xml

<field name="standard_id" on_change="on_change_standard(standard_id)" widget="selection"/>
<field name="group_id" widget="selection"/>

推荐答案

您可以编辑代码并将域添加到'group_id'字段中

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}

它会为您服务

再见

这篇关于如何检索Many2one字段中的值作为选择字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆