How to Read All Fields Data of a Module in Odoo

Chinmay Roy
2 min readDec 16, 2024

--

In Odoo, accessing and analyzing data fields within a model is a critical step for debugging, customizing, or understanding the data structure of the module. In this blog, I’ll explore how to override the read method of a model to extract and print all record field data for logging or debugging purposes.

Understanding the read Method

The read method in Odoo is responsible for retrieving data from the database for the specified fields of a record. When extended or overridden, it allows developers to customize the way data is fetched or processed. This makes it a powerful tool for debugging or auditing.

Use Case: Reading All Fields in a Model

To understand how to retrieve all field data of a model, let’s consider an example using the account.move model. Here’s a scenario where we override the read method to log all the field values of records being read.

Code Implementation

from odoo import models, api
class AccountMoveExtended(models.Model):
_inherit = 'account.move'

# Override read method
def read(self, fields=None, load='_classic_read'):

# Call the original read method
result = super(AccountMoveExtended, self).read(fields=fields, load=load)

# Log the result from the original read method
print(f"""
Account Move result: {result}
""")
return result

Explanation

  1. Calling the Super Method
  • The super method ensures that the default functionality of the read the method remains intact. This is important to avoid breaking Odoo's core behavior.

2. Logging Results

  • After calling the super method, we log the result, which contains the data retrieved by the read method.

Reading All Fields Dynamically

To dynamically read and log all fields of a record without explicitly listing them, you can leverage the fields_get method to retrieve all field names:

from odoo import models, api
class AccountMoveExtended(models.Model):
_inherit = 'account.move'
def read(self, fields=None, load='_classic_read'):
if fields is None:
fields = list(self.fields_get().keys()) # Get all field names

for rec in self:
print(f"Record {rec.id} Data:")
for field in fields:
print(f"{field}: {getattr(rec, field, 'N/A')}")

result = super(AccountMoveExtended, self).read(fields=fields, load=load)
print(f"Account Move result: {result}")
return result

Caution

  1. Avoid Overhead: Reading all fields for large datasets can be resource-intensive. Use this approach only for debugging or auditing purposes.
  2. Respect Permissions: Ensure that the method respects access permissions, as unauthorized access to sensitive data can lead to security issues.
  3. Use Logging Instead of Print: Replace print statements with the Odoo logger (_logger) for better logging practices.

Conclusion

Overriding the read method in Odoo provides a powerful way to gain visibility into a model’s data. By dynamically fetching and logging all field data, developers can streamline debugging and better understand the structure of their models. Use this technique wisely to maintain system performance and security.

--

--

Chinmay Roy
Chinmay Roy

No responses yet