Enable Dark Mode!
how-to-create-name-search-function-in-odoo-17.jpg
By: Unnimaya CO

How to Create Name Search Function in Odoo 17

Technical Odoo 17

We're familiar with Odoo and how relational fields link different models. When filling these fields, we often search for specific values in a large set of records. Odoo's name search function simplifies this by allowing us to customize searches to suit our needs.

This blog will explore the name search function in detail, examining its various use cases thoroughly.

In Odoo 17, the name search function is utilized to locate records within a model connected through a relational field. You can conduct this search using incomplete values or by utilizing fields other than the default "name" field in the record. While Many2one fields allow default name-based searches, the name_search function enables more customized searches by incorporating various parameters or criteria beyond the standard name search.

Let's examine an Odoo illustration. When entering a customer's name into the Customer field of a new sales order quotation or invoice, you can perform a search within the existing records.

how-to-create-name-search-function-in-odoo-17-1-cybrosys

Moreover, it is feasible to search for that particular record by entering the contact's email ID, as demonstrated in the example below.

how-to-create-name-search-function-in-odoo-17-2-cybrosys

You may also observe that we can perform searches using partial email addresses or names. This capability is achieved by utilizing the name search function.

Let's delve into the definition of the name search function.

@api.model
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None):
   # code
   return self._search(expression.AND([domain, args]), limit=limit,   access_rights_uid=name_get_uid)

* name (str): The pattern for matching the name.

* Arguments (args parameter): A list that provides an optional domain for searching, allowing the specification of additional restrictions. 

* Operator (operator parameter): The domain operator is used for names that match, examples include "like," "ilike," "=", and others. 

* Limit (limit parameter): This option allows you to set the maximum number of records to be returned in the search results. 

* name_get_uid: Utilized either to restrict users or to specify the user in the context of the operation.

If we want to incorporate a phone number in the sale order for consumer search, we must inherit the model and define a name search function in the "res.partner."

class ResPartner(models.Model):
   _inherit = 'res.partner'
   @api.model
   def _name_search(self, name='', args=None, operator='ilike', limit=100, name_get_uid=None):
       args = list(args or [])
       if name:
           args += ['|', '|', ('name', operator, name), ('email', operator, name),
                    ('phone', operator, name)]
           return self._search(args, limit=limit, access_rights_uid=name_get_uid)

The default value for 'args' is configured as None. To tailor the parameters of the name search feature, we have the option to establish a domain and integrate the 'args' list within the function.

Through this customization, it becomes possible to perform contact searches using the phone number as well.

how-to-create-name-search-function-in-odoo-17-3-cybrosys

Multiple options can be provided within the domain, and the dot ('.') operator can be utilized for Many2one fields.

For instance, if you desire to enable the ability to search using the state name, you can achieve this by employing the dot operator on the Many2one field 'state_id' as 'state_id.name'. Consequently, the configuration of the 'args' list would be set up as follows:

args += ['|', '|', '|', ('name', operator, name), ('phone', operator, name), ('email', operator, name), ('state_id.name', operator, name)]

Let's explore another instance of the name search functionality, this time within the 'res.city' model. The 'res.city' incorporates a name_get function that incorporates the zip code into its display name. Consequently, when a Many2one field is associated with this model and a search is initiated, a list features its enhanced display name, including the zip code.

class ResCity(models.Model):
   _name = 'res.city'
   _description = 'City'
   _order = 'name'
   name = fields.Char(string="Name", help='Name of city',         required=True, translate=True)
   zipcode = fields.Char(string="Zip", help='Zip of city')
   country_id = fields.Many2one('res.country', string='Country',
                                help='Country to which the city   belongs to', required=True)
   state_id = fields.Many2one('res.country.state', string='State',
                              help='State to which the city belongs to', domain="[('country_id', '=', country_id)]")
   def name_get(self):
       res = []
       for city in self:
           name = city.name if not city.zipcode else '%s (%s)' % (
           city.name, city.zipcode)
           res.append((city.id, name))
       return res
   @api.model
   def _name_search(self, name='', args=None, operator='ilike', limit=100,                  name_get_uid=None):
       args = list(args or [])
       if not (name == '' and operator == 'ilike'):
           args += ['|', (self._rec_name, operator, name),
                    ('zipcode', operator, name)]
       return self._search(args, limit=limit, access_rights_uid=name_get_uid)

Likewise, we have the ability to tailor and define the name_search function in Odoo, enabling us to fine-tune the search for specific records based on field values within a relational field. This customization serves to streamline the search process, providing a more targeted and efficient outcome.

To read more about creating a name search function in Odoo 16, refer to our blog How to Create Name Search Function in Odoo 16


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