Difference Between Search Vs Search Method

CelineGeorge1 780 views 17 slides Sep 09, 2024
Slide 1
Slide 1 of 17
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17

About This Presentation

In Odoo, both the search and _search methods are used to find records. Search Method is a public, standard search function that users and external parts of the application can access. _search Method is a private method used internally within a class or module for advanced or helper functionality.


Slide Content

Difference between search vs _search method Enterprise

Introduction Enterpr ise In Odoo, both the search and _search methods are used to find records. Search Method is a public, standard search function that users and external parts of the application can access. _search Method is a private method used internally within a class or module for advanced or helper functionality.

Search method Enterprise In numerous programming frameworks and libraries, especially those dealing with search functionality, you might come across both search and _search methods. In Odoo, the search method is a function used to retrieve records from the database that match specific criteria. It is part of the Odoo ORM (Object-Relational Mapping) framework and is used to query models.

search() Enterprise The search method in Odoo is primarily used for two types of searches: Empty Search Conditional Search

Search() Enterprise Empty Search When search() is called with empty parameters, it fetches all records from the specified model. This is useful when you need to retrieve every record without any filtering criteria. Conditional Search When criteria or conditions are specified within the parentheses of search(), it returns all records that match the provided criteria. This allows for filtering records based on specific fields and values.

Empty search Enterprise Code partners = self.env['res.partner'].search([]) print("partners:", partners) Explanation The .search([]) retrieves all records from the res.partner model, which represents contacts in Odoo. The empty list [] as the search domain means no filters are applied, so it fetches every contact in the system. The resulting records are stored in the partners variable as a collection of all partner records.

Explanation Enterprise This method is used to search for records in the specified model. The argument is a domain, which is a list of conditions to filter the records. An empty list as the domain means no filtering conditions are applied. This results in fetching all records from the res.partner model.

Conditional Search Enterprise “Conditional search” refers to retrieving records from a database that meet specific criteria or conditions. In Odoo, this is commonly done using the search method with a domain filter.

Example Enterprise partners = self.env['res.partner'].search([('name', '=', 'John Doe')]) print("partners:", partners) In this example: This performs a search with a domain filter that specifies the name field should be equal to "John Doe". The domain is a list of tuples, where each tuple represents a condition. The condition ('name', '=', 'John Doe') checks if the name field of the record is exactly "John Doe".

_search() Enterprise The _search() method is designed to returns an SQL query which can be executed safely or be modified/combined to get the required data .

Example Enterprise comodel = self.env['res.partner'] domain = [('name', '=', 'John Doe')] ids2 = comodel._search(domain) print("Query to be executed which will return table with columns where the name is 'John Doe':", ids2)

Explanation Enterprise Comodel is the res.partner model, which represents contacts. This domain specifies that we are searching for records where the name field is "John Doe". The _search method is called on the res.partner model with the defined domain. This returns a query which when executed will return a table with rows and columns of the res.partner table where the name of the contact is "John Doe".

Difference Enterprise The primary distinction between the search and _search methods is as follows: search: Returns a recordset. _search: Returns a query safe for execution or can be c ombined /modified as per your needs .

Explanation Enterprise search : Commonly used in Odoo modules to retrieve and manipulate records. It is easier for developers to use because it directly provides records to work with. _search : Used internally by Odoo and can be overridden to customize search behavior. You can combine/modify the query to manipulate and extract the data/information you require .

Difference Enterprise Use search when you need to retrieve records to read or manipulate directly, as it provides a convenient and straightforward way to work with recordsets. Use _search if you need to perform a custom search operation or if you are extending Odoo’s functionality and need to modify the search behavior at a lower level. It’s particularly useful for internal logic where you might need to handle the data directly before performing further operations.

Enterprise Conclusion Hence we can conclude that both the search and _search methods are valuable and essential part of Odoo’s development framework and ecosystem .

For More Info. Check our company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com