How to Create a Name Search Function in Odoo 19
Introduction
Odoo is widely known for its powerful ORM and flexible relational fields that allow developers to connect models and manage data efficiently. One of the most common relational fields used in Odoo is the Many2one field, which allows users to select a record from another model.
When working with relational fields, users often need to search through many records to find the correct one. By default, Odoo allows searching using only the display name of the record. However, in real business scenarios, users might want to search using other fields such as email, phone number, or reference codes.
To solve this problem, Odoo provides the name_search() method, which allows developers to customize how records are searched when users type inside relational fields.
In this blog, we will explore how to override the name_search() method in Odoo 19 to allow searching records using multiple fields.
Understanding the name_search() Method
The name_search() method is automatically triggered when a user searches for a record in relational fields such as:
Many2one fields
Many2many selection fields
Reference fields
When the user starts typing inside a Many2one field, Odoo sends a request to the server and calls the name_search() method of the corresponding model.
The method then searches the database and returns matching records in the format:
[(id, display_name)]
These results are displayed in the dropdown suggestion list.
Default Behavior of Name Search
If the name_search() method is not customized, Odoo searches only the display name field of the model.
This display name is defined using the _rec_name attribute.
Example:
_rec_name = "name"
In this case, Odoo will search only using the name field.
While this works well for small datasets, it becomes limiting when users need to search using other identifiers such as email or phone numbers.
Use Case
Consider a scenario where we manage Student Registrations in Odoo.
Each student has the following fields:
Student Name
Email
Phone Number
By default, Odoo allows searching students only by their name. However, users might want to search students using:
Email address
Phone number
To enable this functionality, we override the name_search() method.
1. Creating the Student Registration Model
First, we create a model to store student information.
2. Creating view for Student Registration Model
3. Creating Student Course Model
4. Creating View for Student Course Model
5. Access Rights
How the Name Search Works in the UI
When a user opens the Student Course form, the Student field appears as a dropdown.
When the user starts typing, Odoo triggers the customized name_search() method.
Users can search using:
Student name
Email address
Phone number
Even if the user types an email or phone number, the correct student record will appear.
Frontend Output
Here is the frontend view of student_model_view.xml where we can able to add the details of the students in the form view and stored to the database.
And after that we can access the record here in this student_course_view.xml which is a representation of many2one fields from the student_model.py used in student_course.py where the name search is defined.
This is the dropdown showing the name of all the records that are provided.
Here accessing the name of the record by simply entering the name.