Enable Dark Mode!
name-search-function-in-odoo-15.jpg
By: Aneesh K

Name Search Function in Odoo 15

Technical Odoo 15

We are familiar with Odoo and the use cases of relational fields in Odoo to create relational links between different models. So to assign values in these relational fields, we have to search and find the required value from the list of all the available records. The name search function in Odoo helps make this search more accessible and also to customize the searching feature based on our requirements. 

In this blog, we will discuss the name search function and its use cases in detail.

Name search function is used in Odoo to search records of a model in a relational field using a partial value or using fields other than the default name of the record. For example, when we search the records in a Many2one field, by default when we type the name of the record we can search and select from the existing records. But if we want to search using some other parameters, we can use the name_search function. 

Let’s see an example in Odoo. When we create a new sale order quotation or a new invoice, in the Customer field, we can search from existing records when we type the customer’s name.


name-search-function-in-odoo-15


Also when we type the email ID of the contact, we can search that particular record.

In the example, the email address of the contact.


name-search-function-in-odoo-15


If we search in the Customer’s field using this email address, we can get the list of contacts having this email address.


name-search-function-in-odoo-15


Also, you can see that we can search using a partial value of name or email. This property is achieved using the name search function.

Let’s see how the name search function is defined.

    @api.model

    def _name_search(self, name='', args=None, operator='ilike', limit=100, 

name_get_uid=None):

#code

           return self._search(args, limit=limit, access_rights_uid=name_get_uid)

The parameters of the function are :

- name (str) – The name pattern to match

- args (list) – Optional search domain to specify further restrictions

- operator (str) – Domain operator for matching names, such as 'like', ‘ilike’, ‘=’, etc.

- limit (int) – Optional to limit a maximum number of records to be returned.

- name_get_uid - To specify the user or to restrict users.


Suppose we require that we have to also add a phone number for searching the customers. For that, we have to define a name search function in the ‘res.partner’ by inheriting the model. 


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)


By default, the value of args is given as None. We can provide the conditions we need for the name search function as a domain and append the args list inside the function.

The domain criteria are given in the format (‘field_name’, operator, value).

Now we can search the contacts using the phone number also


name-search-function-in-odoo-15



You can provide multiple options in the domain and use the dot (‘.’) operator for Many2one fields.

For example, if you want to add the option to search using the state name, which can be accessed by calling the dot operator on the Many2one field ‘state_id’ as ‘state_id.name’. So the args list becomes -

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


Let’s see another example of the name search function in the ‘res.city’ model.

The ‘res.city’ uses a name_get function and adds zip code to its display name.

So when a Many2one field is defined to this model and when we try to search, we can see a list with its display name.

class City(models.Model):

    _name = 'res.city'

    _description = 'City'

    _order = 'name'


    name = fields.Char("Name", required=True, translate=True)

    zipcode = fields.Char("Zip")

    country_id = fields.Many2one('res.country', string='Country', required=True)

    state_id = fields.Many2one(

        'res.country.state', 'State', 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 [])

        # optimize out the default criterion of ``ilike ''`` that matches everything

        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)


But we can search among the records using the zipcode because of the name_search function defined in the model. You can also see that in the name_search function, we used a self._rec_name in this case and provided an if condition to optimize out the default criterion of “ ilike '' that matches everything.

So like this, we can customize and define the name_search function in Odoo to search the specific records by some field values in a relational field.


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



0
Comments



Leave a comment

 
whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

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