简体   繁体   English

如何检索Many2one字段中的值作为选择字段?

[英]How to retrive values in Many2one field as selection field?

How to retrieve values on Many2one field using OnChange ? 如何使用OnChange检索Many2one字段上的值?

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 这是我的xml

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

you can edit your code and add domain to 'group_id' field like this 您可以像这样编辑代码并将域添加到'group_id'字段

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

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)]"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM