For this recipe, we will use the following model definition:
class LibraryBook(models.Model): _name = 'library.book' name = fields.Char('Title') isbn = fields.Char('ISBN') author_ids = fields.Many2many('res.partner', 'Authors') @api.multi def name_get(self): result = [] for book in self: authors = book.author_ids.mapped('name') name = '%s (%s)' % (book.name, ', '.join(authors)) result.append((book.id, name)) return result
When using this model, a book in a Many2one widget is displayed as Book Title (Author1, Author2...). Users expect to be able to type in an author's name and find the list filtered according to this name, but this will not work since the default implementation of name_search only uses the attribute referred to by the _rec_name attribute of the model class, which in our case is 'name'. We also want to allow filtering by ISBN number.