Dayana
Dayana

Reputation: 1538

Show a many2many field as a many2one

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?

Upvotes: 2

Views: 1617

Answers (1)

Charif DZ
Charif DZ

Reputation: 14721

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

Upvotes: 2

Related Questions