Chapter 15 - Odoo 15 Development Book

Grouped data

In some cases need datas on a groped forms, like montly reports, sales reports etc.. like that.Manually searching and grouping records is time-consuming.We'll look at how to obtain grouped data with the read group() method in this recipe.

The read_group() method is mainly used for smart buttons and statistical datas.

In such cases we can shows the number of sale orders on the partner form. Search on the sale orders for the customers and find the count of the sale orders.

For example: on the partner model

sale_count = fields.Integer(compute='_compute_sale_count',
string='Sale count')
def _compute_sale_count(self):
so = self.env['sale.order'].search(domain=[('partner_id', 'in', self.ids)])
for partner in self:
partner.sale_count = len(so.
filtered(lambda sale: ==

In this example we can see that, When the ssale_count field is displayed in the tree view, it will fetch and filter sales orders for all of the partners in the list.The read_group() technique won't make much of a difference with this modest amount of data, but if the amount of data expands, it could become a problem.The read_ group method can be used to solve this problem.

There is another example for this but that will only consumes one SQL query, even for large datasets:

# on the partner model

sale_count = fields.Integer(compute='_compute_sale_count',
string='Sale count')
def _compute_sale_count(self):
so_data = self.env['sale.order'].read_group(
domain=[('partner_id', 'in', self.ids)],
fields=['partner_id'], groupby=['partner_id'])
mapped_data = dict([(m['partner_id'][0], m['partner_
id_count']) for m in so_data])
for partner in self:
partner.sale_count = mapped_data[]

In this example, Internally, the read group() method employs SQL's GROUP BY capability.This speeds up the read group method, even with enormous datasets.The Odoo web client utilises this function internally in the charts and grouped tree view. Using different arguments, you can change the behaviour of the read group method.

different parameters available for the read_group method

  • domain : This is how records are filtered.This is where you'll look for the read group method.
  • fields : This is a list of the fields that the grouping will retrieve.Unless you utilise aggregate functions, the fields stated below should be in the groupby parameter.
  • groupby : This option will be a list of fields that will be used to group the records.It allows you to sort records by several fields.
  • offset : This option is used to paginate the results.This argument is useful if you wish to skip a few records.
  • limit : This is a pagination option that specifies the maximum number of records to get.
  • lazy : Boolean values are accepted for this parameter.Its value is True by default. If this parameter is set to True, just the first field in the groupby argument is used to group the results.


Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635



Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.



Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message