我有一个字段many2many,在一个特定的视图中,我需要将它显示为一个many2one,或者模仿many2one字段的行为(限制只能添加一个记录,如果用户选择另一个记录,那么先前选择的记录将被删除)。在我看来:
<field name="employee_ids" widget="many2one" />但它给了我以下错误:
TypeError:'int‘对象不可迭代
有办法做到这一点吗?
发布于 2017-11-21 20:37:00
我认为您可以通过使用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 recordhttps://stackoverflow.com/questions/47418172
复制相似问题