Odoo’s ORM (Object-Relational Mapping) layer provides multiple ways to access data stored in the database. While most developers rely on direct field access or methods like search() and browse(), the read() method remains one of the core low-level tools for retrieving record data. Understanding how read() works is essential for building efficient backend logic, debugging, and optimizing performance in Odoo 19.
This article explains what the read() method is, how it works, when to use it, and best practices for Odoo developers.
What is the read() Method?
The read() method is used to fetch field values from records and return them as a list of dictionaries. Each dictionary represents one record, with keys as field names and values as field values.
Basic syntax:
records.read(fields=None, load='_classic_read')
Parameters:
- fields: list of field names to retrieve. If not provided, all readable fields are returned.
- load: controls how relational fields are formatted. Default is _classic_read.
Return type:
[
{'id': 1, 'name': 'John', 'email': 'john@email.com'},
{'id': 2, 'name': 'Jane', 'email': 'jane@email.com'}
]
Basic Example
Assume you want to read employee names and work emails.
employees = self.env['hr.employee'].search([])
data = employees.read(['name', 'work_email'])
Result:
[
{
"id": 21,
"name": "Barty McBly",
"work_email": "barty.mcbly@example.com"
},
{
"id": 20,
"name": "Beth Evans",
"work_email": "beth.evans77@example.com"
}
]
This retrieves only the specified fields, which improves performance compared to loading everything.
When Should You Use read()?
1. When You Need Raw Data Format
read() is useful when returning structured data, such as for APIs or controllers.
Example:
@http.route('/employees', type='json', auth='user')
def get_employees(self):
employees = request.env['hr.employee'].search([])
return employees.read(['name', 'work_email'])
This returns JSON-ready data immediately.
2. When Optimizing Performance
If you only need specific fields, read() avoids loading unnecessary data.
Example:
self.env['res.partner'].search([]).read(['name'])
Instead of loading all fields.
This reduces memory usage and improves speed
3. Debugging and Inspecting Records
read() helps inspect exact stored values.
Example:
record = self.env['res.partner'].browse(1)
print(record.read())
This shows all readable fields and values
How Relational Fields Are Returned
Relational fields such as Many2one are returned as a tuple:
Example:
employees.read(['country_id'])
Result:
[
{'id': 1, 'country_id': (233, 'Singapore')}
]
Format:
(field_id, display_name)
This makes relational data readable without extra queries.
Conclusion
The read() method is a core part of Odoo’s ORM that allows developers to retrieve record data in a structured dictionary format. While direct field access is preferred for most business logic, read() is essential when working with APIs, controllers, debugging, or performance-sensitive operations.
Understanding when and how to use read() helps developers write cleaner, faster, and more efficient Odoo modules. In Odoo 19, this method continues to play an important role in bridging database records and structured output, especially in backend integrations and data serialization scenarios.
Mastering this method gives developers deeper control over how data is retrieved and handled inside Odoo.
