我有一个many2many字段,在特定 View 中我需要将其显示为many2one,或者模仿many2one字段的行为(限制只能添加一条记录,如果用户选择另一条记录,则该记录将被添加)之前选择的将被删除)。在我的观点中声明:
<field name="employee_ids" widget="many2one" />
但它给了我以下错误:
TypeError: 'int' object is not iterable
有什么办法可以实现这一点吗?
最佳答案
我认为你可以强制用户仅选择一条记录 使用 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
关于odoo - 将多对多字段显示为多对一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47418172/