How to search many2one field by other than "name" field.

Problem: We know that in Odoo search is available only on "name" field. For example here is many2one field named "section". And we are enable to search on those field because this field is the combination of two field subject name and section name.






Solution: We can enable search on many2one fields by other than "name" field. We can achieve this by overriding "name_search" method of Odoo.

Code:

    1@api.model
    2def name_search(self, name, args=None, operator='ilike', limit=100):
    3    args = args or []
    4    recs = self.browse()
    5    if not recs:
    6        recs = self.search([('subject_id', operator, name)] + args, limit=limit)
    7    return recs.name_get()

Description:

Here we override "name_search" builtin method of Odoo. And in line no 7 we searched on "subject_id" instead of name.