Odoo is renowned for its powerful ORM and relational fields, which facilitate data linking between various models. Users frequently must search through a lot of records to find the correct value when working with these relational fields, particularly Many2one fields. Odoo's built-in name based search makes this process easier by default.
By offering a strong framework for customizing search logic to meet particular business needs, Odoo 19 further improves the usability of relational fields. Developers can make more user-friendly and effective search experiences by overriding the name_search function, which enables users to find records fast even in complicated or data-rich environments.
In this blog, we'll look at how to add a custom name_search function to Odoo 19 and show how it can greatly enhance the usability and accuracy of searches in relational fields.
Understanding name_search function
The name_search function in Odoo provides a flexible way to find records using partial values or fields other than the default name field. By customizing this function, we can enhance the search experience by supporting multiple search criteria, making record selection more accurate and efficient.
The function follows this structure:
@api.model
@api.readonly
def name_search(self, name: str = '', domain: DomainType | None = None, operator: str = 'ilike', limit: int = 100):
//code
return self.search_fetch(domain, ['display_name'], limit=limit)
Parameters:
name: the name pattern to match
domain: search domain. Use an empty list to match all records.
operator: domain operator for matching name, such as 'like' or '='.
limit: max number of records to return
Implementing name_search in Odoo 19
This method searches for records whose display name matches the provided pattern using the specified operator, while also satisfying an optional search domain. It is commonly used to offer suggestions when users enter partial values in relational fields.
Although it generally behaves as the reverse of the display_name computation, this behavior is not strictly guaranteed. Internally, this method is equivalent to performing a search() using a domain derived from display_name and then returning the corresponding record IDs along with their display names.
When we want to search partners not just by name, but also with their phone number or email in res.partner we can use name_search.
class ResPartner(models.Model):
_inherit = 'res.partner'
@api.model
def name_search(self, name='', args=None, operator='ilike', limit=100):
args = args or []
domain = []
if name:
domain = [
'|', '|',
('name', operator, name),
('email', operator, name),
('phone', operator, name),
]
final_domain = args + domain
partners = self.search(final_domain, limit=limit)
return [(partner.id, partner.display_name) for partner in partners]
To ensure the method works even without any extra domain we pass argos as none.
The domain allows searching across multiple fields and improves usability when records are large.
In the final_domain odoo’s existing filters, along with the custom domain, are combined, and then executes a combined database search with limited records. The name_get() formats the record and returns the result.
In conclusion, Odoo's **name_search** function provides an effective means of improving record lookup in relational fields. When working with large datasets, this method can be customized by developers to enable searches across multiple attributes, including name, email, phone, VAT, and more, making record selection quicker and more user-friendly. More focused searches that closely match actual business needs are made possible by this flexibility. You can greatly increase the usability and efficiency of all of your Odoo applications by efficiently extending the name_search function.
To read more about How to Create a Name Search Function in Odoo 18, refer to our blog How to Create a Name Search Function in Odoo 18.