๐Ž๐‘๐Œ ๐Œ๐ž๐ญ๐ก๐จ๐๐ฌ : search_read()

amr gaberamr gaber
2 min read

It combines the functionality of the search and read functions, allowing you to perform a search and then read the selected records in one step.

Parameters

  • domain: A search domain.

  • fields: A list of fields to read from the resulting records.

  • offset: The number of records to ignore.

  • limit: The maximum number of records to return.

  • order: Columns to sort result.

  • read_kwargs: Additional keyword arguments to be passed to the read() method.

Return

Returns a list of dictionaries, where each dictionary represents a single record.

How it is efficient?!

  • Avoids the need to make multiple separate calls to the database.

  • Allows you to filter the results of a search based on specific fields.

For More Efficiency

  • Ensure that you include only the necessary fields while reading, as the default is to retrieve all fields if no field is added.

  • Use the offset and limit arguments to paginate the results. This can be useful for fetching large amounts of data without overloading the server.

Example

# ORM METHODS

# search_read()

res_users = self.env['res.users'].search_read(
    domain=[('create_uid.email', '!=', False)],
    fields=['create_uid', 'name', 'login'],
    order='create_uid asc',
    limit=5
)

print(res_users)

# return list with below dict
#> ('create_uid': (2, 'Admin'), 'id': 11,
# 'login': 'api_user1@test.com', 'name': 'api_user1'}

#> {'create_uid': (2, 'Admin'), 'id': 8, 
# 'login': 'api_user', 'name': 'api_user'}

#> {'create_uid': (1, 'OdooBot'), 'id': 6, 
# 'login': 'demo', 'name': 'Marc Demo'}
#> {'create_uid': (1, 'OdooBot'), 'id': 7, 
# 'login': 'portal', 'name': 'Joel Willis'}
#> {'create_uid': (1, 'OdooBot'), 'id': 2, 
# 'login': 'admin', 'name': 'Mitchell Admin'}

If you need to check how search and read methods separately work, refer to the following links:

0
Subscribe to my newsletter

Read articles from amr gaber directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

amr gaber
amr gaber

Hey! I'm Amr, a software engineer who is passionate about all things open source. I enjoy coding using Python.