【问题标题】:Show a many2many field as a many2one将 many2many 字段显示为 many2one
【发布时间】:2017-11-21 16:37:50
【问题描述】:

我有一个字段 many2many 并且在特定视图中我需要将其显示为 many2one,或模仿 many2one 字段的行为(限制只能添加一条记录,如果用户选择另一条记录,则之前选择的将被删除)。在我声明的视图中:

<field name="employee_ids" widget="many2one" />

但它给了我以下错误:

TypeError: 'int' 对象不可迭代

有什么办法可以做到吗?

【问题讨论】:

  • 所以你想让用户只选择一条记录?!!
  • 你可以使用&lt;field name="employee_ids" widget="selection" /&gt;它只允许选择一条记录。
  • 为什么不能简单地使用Many2one 字段?什么要求您使用Many2many?或者这是一个已经存在的字段而不是自定义字段?
  • @travisw 这是一个现有字段,我无法更改类型。
  • @Heroic 当我尝试选择小部件时,它给了我同样的错误。

标签: openerp odoo-9 odoo-view


【解决方案1】:

我认为您可以通过以下方式强制用户仅选择一条记录 使用 onchange 装饰器:

@api.onchange('employee_ids')
def force_one_selection(self):
    """Force the user to select only one record"""
    if self.employee_ids and len(self.employee_ids) > 1:
        # user has added a new record
        self.employee_ids = [(6, 0, self.employee_ids[0].id)] # you can change the index to 1
        # and you can return a warning here to tell the user that he should not select more than 
        # one record

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    相关资源
    最近更新 更多