我有一个字段many2many并且在特定视图中我需要将其显示为many2one,或模仿many2one字段的行为(限制只能添加一条记录,如果用户选择另一条记录,那么之前选择的将被删除)。在视图中我声明:
<field name="employee_ids" widget="many2one" />
但它给了我以下错误:
TypeError:'int'对象不可迭代
有没有办法实现这个目标?
答案 0 :(得分:2)
我认为您可以强制用户只选择一条记录 使用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