Difference between search
vs _search method
Enterprise
Introduction
Enterprise
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 combined/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

Difference Between Search Vs Search Method

  • 1.
    Difference between search vs_search method Enterprise
  • 2.
    Introduction Enterprise In Odoo, boththe 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.
  • 3.
    Search method Enterprise ● Innumerous 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.
  • 4.
    search() Enterprise The search methodin Odoo is primarily used for two types of searches: ● Empty Search ● Conditional Search
  • 5.
    Search() Enterprise ● Empty Search Whensearch() 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.
  • 6.
    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.
  • 7.
    Explanation Enterprise This method isused 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.
  • 8.
    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.
  • 9.
    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".
  • 10.
    _search() Enterprise The _search() methodis designed to returns an SQL query which can be executed safely or be modified/combined to get the required data .
  • 11.
    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)
  • 12.
    Explanation Enterprise ● Comodel isthe 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".
  • 13.
    Difference Enterprise The primary distinctionbetween the search and _search methods is as follows: search: Returns a recordset. _search: Returns a query safe for execution or can be combined/modified as per your needs .
  • 14.
    Explanation Enterprise search: Commonly usedin 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 .
  • 15.
    Difference Enterprise ● Use searchwhen 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.
  • 16.
    Enterprise Conclusion Hence we canconclude that both the search and _search methods are valuable and essential part of Odoo’s development framework and ecosystem .
  • 17.
    For More Info. Checkour company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com