1
votes

I have a field many2many and in a specific view I need to show it as a many2one, or imitate the behavior of the many2one fields (restrict that only one record can be added and if the user select another record, the one that had previously selected will be deleted). In the view I declared:

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

but it gave me the following error:

TypeError: 'int' object is not iterable

Is there any way to achieve this?

1
so you want the user to select only one record?!!Charif DZ
you can use <field name="employee_ids" widget="selection" /> it will allow to select only one record.Heroic
Why can't you simply use a Many2one field? What is requiring you to use Many2many? Or is this an already existing field and not a custom field?travisw
@travisw it is an existing field and I can't change the type.Dayana
@Heroic when I try selection widget it gives me the same error.Dayana

1 Answers

2
votes

I think you can force the user to select only one record by using onchange decorator:

@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