Enable Dark Mode!
name-search-function-in-odoo-14.jpg
By: Cirin C Baby

Name Search Function in Odoo 14

Technical Odoo 14

When we create a sales quotation from the sales module you need to add a customer in the customer field. For selecting a specific customer, we start typing something in that field, maybe it’s customer name or customer email address. While we type the customer name partially it shows us some suggestions of record that match the given value. That is, it shows a suggestion of customer records, whose name contains the value we typed in that field. Similarly, when we partially enter an email address it also shows some suggestions of records whose email address matches with the entered value.

So how is this search performed inside odoo? When we give a partial value how does it suggest some records, also when we give an exact value (exact customer name) how does it return that customer record?

So this search functionality inside a relational field is accomplished by the Name Search function.

Name search function is an odoo ORM method that is responsible for searching the specific records by some field values in a relational field.

This is used for example to provide suggestions based on a partial value for a relational field.

name-search-function-in-odoo-14-cybrosys

Let’s take a look at the name search function

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

Parameters

name (str) – the name pattern to match

args (list) – optional search domain to specifying further restrictions

operator (str) – domain operator for matching name, such as 'like', ‘ilike’, ‘=’, etc..

limit (int) – optional max number of records to return

name_get_uid - to specify the user, to restrict users

similarly we can also add custom search in fields by overriding _name_seach function.

Let’s take a case where you need to search a customer by customer phone number from the customer field in the sales quotation. 

name-search-function-in-odoo-14-cybrosys

These are some customer records, we can search this record by name and email from the customer field in the sales quotation. What if we need to search for a customer by phone number?

name-search-function-in-odoo-14-cybrosys

When we search for a phone number corresponding to the customer Azure Interior, It does not show any records.

So we can search the record by phone number by redefining the Name Search function

For that, you need to inherit the corresponding model and override the name search function there

In our case we need to inherit ‘res.partner’ models since we need to apply search on customer field in the quotation and that field is related to the res.partner model.

That is we need to apply searches on customer records, so that customer model(‘res.partner’) needs to be inherited.

After inheriting the mode, override the _name_search function

Let’s look at the below example code

name-search-function-in-odoo-14-cybrosys

This method is equivalent to calling search() method with a search domain based on display_name.

By default the value of args is given as None, We can assign an optional search domain in args for further restrictions

The function checks if there is any value assigned in args, else an empty list is assigned to args.

We need to add a domain inside the function, based on this domain the search will perform.

A domain is a list of criteria. Each criteria contains three items 

 . field_name : A field name of the current model, use dot ‘.’ operator if Many2one field

       Eg: street, city.name

. operator : operator used to compare the field name with value

      Eg: like, ilike, =, <, >, etc..

.value : This is the input value, this value compared with the value in the given field

    

Add criteria like this  (field_name, operator, value)

          Eg:  (‘street’, ‘ilike’, ‘test’) This will returns all the customer records its name field         contains ‘test’ string


Here we added a domain so the records can search by name, phone, and email field values.

Now if we search a customer by phone number it will show the matching customers

name-search-function-in-odoo-14-cybrosys

Likewise, if you need to search for more fields, you can just add them to the domain.

For search by state name, just add the domain like this

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

From now on you can also search customer by city name


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

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