【问题标题】:How to retrive values in Many2one field as selection field?如何检索 Many2one 字段中的值作为选择字段?
【发布时间】:2015-04-06 16:16:47
【问题描述】:

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

学生应注册在一个标准和一个组...该标准有多个组 所以我希望当我更改字段标准时.. 组字段应该使用该标准中的组进行更新

当我尝试这样做时,它给了我一个错误

'Expected singleton: fci.standard.groups(3, 4, 5, 6)' 

我正在尝试当我更改标准字段时,组字段将更新为仅选择此标准中的组

这是我的字段

'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"/>

【问题讨论】:

  • 您可以使用域来执行此操作。但是你需要删除widget="selection"。动态域不能与 widget="selection" 一起使用。选择小部件只会忽略动态域。

标签: python xml openerp onchange odoo


【解决方案1】:

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

<field name="standard_id" widget="selection"/>
<field name="group_id" widget="selection" domain[('standard_id','=',standard_id)]"/>

并像这样编辑你的函数

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}

它会为你工作

再见

【讨论】:

    【解决方案2】:

    无需为此编写更改方法,您可以通过将域应用于该字段来实现。尝试关注,

    <field name="standard_id" widget="selection"/>
    <field name="group_id" widget="selection" domain="[('standard_id','=',standard_id)]"/>
    

    【讨论】:

    • 动态域不能与 widget="selection" 一起使用。选择小部件只会忽略动态域。
    • 好的,然后在 on_change 方法中更改一件事,val.update({'group_id': [ g.id for g in student_data.groups_ids ]})
    • 为什么on_change不能同时刷新2个字段?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多