Enable Dark Mode!
how-to-create-a-name-search-function-in-odoo-18.jpg
By: Anfas Faisal K

How to Create a Name Search Function in Odoo 18

Technical

Introduction

Odoo provides powerful relational fields that allow seamless linking between models. When users interact with Many2one fields, they often need to search for specific records efficiently. By default, Odoo searches for records based on their name field. However, the name_search function enables customization, allowing searches based on multiple fields such as email, phone number, or other relevant details.

In this blog, we will explore how to create a custom name_search function in Odoo 18. This function enhances search capabilities in relational fields, making data retrieval more efficient and user-friendly.

Understanding the Name Search Function

The name_search function in Odoo is a powerful tool for searching records with incomplete values or by using fields beyond the default name field. By customizing this function, we can improve the search process to include multiple criteria.

Function Definition

The name_search function follows this structure:

@api.model
def name_search(self, name, args=None, operator='ilike', limit=100):
    # Function implementation
    return self._search(expression.AND([domain, args]), limit=limit)

Parameters:

* name (str): The pattern to match in the search.

* args (list): Additional filters to refine the search.

* operator (str): Specifies the search operation (ilike, =, like, etc.).

* limit (int): Maximum number of records to return.

Implementing Name Search in Odoo 18

Let's consider an example where we want to allow users to search for a customer using their name, email, phone number in the res.partner model.

Customizing Name Search in res.partner

Below is the updated name_search function implemented in Odoo 18:

from odoo import api, models
class ResPartner(models.Model):
   _inherit = 'res.partner'
   @api.model
   def name_search(self, name='', args=None, operator='ilike', limit=100):
       args = list(args or [])
       if not name:
           # When no name is provided, call the parent implementation
           return super().name_search(name=name, args=args, operator=operator,
                                      limit=limit)
       # Add search criteria for name, email, and phone
       domain = ['|', '|',
                 ('name', operator, name),
                 ('email', operator, name),
                 ('phone', operator, name)]
     # Combine with existing args
       if args:
           domain = ['&'] + args + domain
       # Use search_fetch to get both IDs and display_name efficiently
       partners = self.search_fetch(domain, ['display_name'], limit=limit)
       # Return in the expected format: [(id, display_name), ...]
       return [(partner.id, partner.display_name) for partner in partners]

Explanation of the Code:

* The function first converts args into a list to ensure it's mutable.

* If no name is provided, it calls the default name_search function from the parent class.

* A custom domain is defined to search by name, email, and  phone fields using the given operator.

* If additional arguments (args) exist, they are combined with the domain using the AND condition.

* The function retrieves matching records and returns them using name_get() to ensure proper formatting.

This customization enables contact searches by email and phone number.

How to Create Barcodes in Odoo 18-cybrosys

Conclusion

The name_search function in Odoo 18 provides a powerful way to enhance searches within relational fields. By implementing a custom name_search, we can improve user experience by allowing searches using multiple attributes such as name, email, phone, VAT, and more. This customization is particularly beneficial for handling large datasets and making the search process more intuitive.

By following the steps outlined in this blog, you can implement and extend the name_search function in your Odoo applications to meet specific business requirements.

To read more about How to Create Name Search Function in Odoo 17, refer to our blog How to Create Name Search Function in Odoo 17.


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
Kakkancherry, 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