Enable Dark Mode!
how-to-create-a-name-search-function-in-odoo-19.jpg
By: Yadu Krishna N P

How to Create a Name Search Function in Odoo 19

Technical Odoo 19 Odoo Enterprises Odoo Community

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.


Frequently Asked Questions

What is the purpose of the name_search function in Odoo 19?

The name_search function is used to search records in relational fields such as Many2one. By default, Odoo searches using the display_name, but by overriding name_search, developers can extend the search logic to include multiple fields like email, phone, VAT, or any other relevant attributes. This improves search flexibility and user experience.

When should I override the name_search method?

You should override the name_search method when the default name-based lookup is not sufficient for your use case. This is especially important when users need to search records using multiple fields such as name, email, or phone number instead of just the display name. It is also useful when working with large datasets where relying only on the default search behavior may lead to inefficient or less accurate results. Additionally, overriding name_search helps when specific business requirements demand more precise or customized search logic, or when you want to enhance usability in relational dropdown fields by making search results more flexible and user-friendly.

Does overriding name_search affect all Many2one fields?

Yes. Once overridden in a model (e.g., res.partner), the custom name_search logic applies to all Many2one fields referencing that model across the system. No additional view changes are required.

What is the role of the args (or domain) parameter in name_search?

The args parameter contains existing filters applied by Odoo, such as access rules, company restrictions, or view-specific domains. When implementing a custom name_search, these filters should be preserved and combined with your custom domain to ensure system rules and security constraints are respected.

How does name_search improve performance and usability?

By allowing targeted searches across multiple fields and limiting the number of returned records, name_search enhances both efficiency and user experience. It enables users to quickly find relevant records using partial input while ensuring fast dropdown suggestions, even in data-heavy environments.

If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, KINFRA Techno Park
Kakkanchery, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message