Understanding the Read Group Method in Odoo

Abid Patel
25-Oct-2024 Updated : 25-Oct-2024

Learn about the Read Group method in Odoo, a powerful way to group and aggregate data for creating summaries and reports in various Odoo modules.

Understanding the Read Group Method in Odoo

The Read Group method in Odoo is a powerful tool used for data aggregation. It enables developers to group records based on specific fields and apply aggregation functions like count, sum, and average. This method is commonly used to display data summaries in list views, kanban views, or custom reports, providing users with valuable insights without manual calculations.

What is the Read Group Method?

The Read Group method in Odoo allows you to perform grouped queries on your database records. With this method, you can retrieve data with specific filters, group them by fields, and apply various aggregation functions for analytics or reporting purposes. It’s a key method when you need summary information or metrics without creating additional views or models.

Syntax of the Read Group Method

The Read Group method can be called on any model using the following syntax:

python
records = self.env['model_name'].read_group(
        domain,             # List of filter criteria
        fields,             # List of fields to retrieve and aggregate
        groupby,            # List of fields to group by
        offset=0, limit=None, orderby=None, lazy=True
    )

Parameters:

  • domain: Filters the data, similar to a search filter.
  • fields: Lists fields to retrieve; some fields may have aggregation functions applied.
  • groupby: Fields used for grouping records.
  • offset, limit, orderby, lazy: Optional parameters to control pagination, sorting, and lazy loading.

Using the Read Group Method: An Example

In this example, we’ll use the Read Group method to get the total sales grouped by salesperson in a custom sales report:

python
sales_summary = self.env['sale.order'].read_group(
        domain=[('state', '=', 'sale')], 
        fields=['user_id', 'amount_total:sum'], 
        groupby=['user_id']
    )

In this example:

  • model='sale.order': We're working with the sales orders model.
  • domain: Filters for records where the state is 'sale'.
  • fields: Retrieves the user_id (salesperson) and applies the sum function on amount_total.
  • groupby: Groups the results by user_id.

Reading the Output of Read Group

The Read Group method returns a list of dictionaries where each dictionary represents a group with aggregated values. Here’s how the result would look:

python
[
        {'user_id': (3, 'Jane Doe'), 'amount_total': 50000.0, '__count': 10},
        {'user_id': (7, 'John Smith'), 'amount_total': 30000.0, '__count': 6}
    ]

In this output:

  • user_id: Contains the salesperson ID and name.
  • amount_total: Shows the sum of sales for each salesperson.
  • __count: Shows the count of orders per salesperson.

Benefits of Using Read Group in Odoo

The Read Group method is ideal for creating summaries, reports, and insights directly within Odoo without requiring extensive custom views or modules. It’s commonly used in dashboard views, kanban summaries, and list views with grouped data.

Conclusion

In summary, the Read Group method in Odoo is an efficient way to perform data aggregation and grouping, allowing you to quickly retrieve and analyze summary information. By understanding its syntax and application, developers can unlock powerful reporting capabilities within Odoo models.

Make a Comment

Your email address will not be published. Required fields are marked *