How to use search_read
method in Odoo 18
Enterprise
Enterprise
Introduction
The search_read method is a convenient way to search for records
that match specific criteria and retrieve their data in a single call.
This method combines the functionality of search() and read(),
reducing the number of requests and simplifying the code.
Let’s see how this method is used on Odoo 18
Enterprise
Basically, Odoo uses search_read() method with the syntax
Model.search_read(domain, field_names[, offset=0][, limit=None])
● domain: A list of tuples defining the search criteria (e.g.,
[('field_name', 'operator', 'value')]).
● fields (optional): A list of field names to retrieve. If not
specified, all fields will be fetched.
● limit (optional): An integer to specify the maximum number of
records to return.
● offset (optional): An integer to specify the number of records to
skip before returning results.
Enterprise
Let’s take a look at the example
partners = self.env['res.partner'].search_read(
domain=[('is_company', '=', True)],
fields=['name', 'email'],
limit=5
)
This code will bring all the 5 records from the partner table with the
fields name and email with the given domain.
Enterprise
It’s better to use search_read() for smaller datasets. For large datasets,
it is better to use search() with batching to avoid performance issues.
Also we need to ensure that the fields specified in the fields parameter
exist in the model, or an error will be raised.
As the search_read() method combines search() and read() into a single
call, it eases the work by reducing overhead. Also, the method is very
convenient and flexible to use.
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

How to use search_read method in Odoo 18

  • 1.
    How to usesearch_read method in Odoo 18 Enterprise
  • 2.
    Enterprise Introduction The search_read methodis a convenient way to search for records that match specific criteria and retrieve their data in a single call. This method combines the functionality of search() and read(), reducing the number of requests and simplifying the code. Let’s see how this method is used on Odoo 18
  • 3.
    Enterprise Basically, Odoo usessearch_read() method with the syntax Model.search_read(domain, field_names[, offset=0][, limit=None]) ● domain: A list of tuples defining the search criteria (e.g., [('field_name', 'operator', 'value')]). ● fields (optional): A list of field names to retrieve. If not specified, all fields will be fetched. ● limit (optional): An integer to specify the maximum number of records to return. ● offset (optional): An integer to specify the number of records to skip before returning results.
  • 4.
    Enterprise Let’s take alook at the example partners = self.env['res.partner'].search_read( domain=[('is_company', '=', True)], fields=['name', 'email'], limit=5 ) This code will bring all the 5 records from the partner table with the fields name and email with the given domain.
  • 5.
    Enterprise It’s better touse search_read() for smaller datasets. For large datasets, it is better to use search() with batching to avoid performance issues. Also we need to ensure that the fields specified in the fields parameter exist in the model, or an error will be raised. As the search_read() method combines search() and read() into a single call, it eases the work by reducing overhead. Also, the method is very convenient and flexible to use.
  • 6.
    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