Accessing grouped data
When you want data for statistics, you often need it in a grouped form, such as a monthly sales report, or a report that shows sales per customer. It is time-consuming to search records and group them manually. In this recipe, we will explore how you can use the read_group()
method to access grouped data.
How to do it...
Perform the following steps:
Note
The read_group()
method is widely used for statistics and smart stat buttons.
- Let's assume that you want to show the number of sales orders on the partner form. This can be done by searching sales orders for a customer and then counting the length:
# in res.partner model so_count = fields.Integer(compute='_compute_so_count', string='Sale order count') def _compute_so_count(self): Â Â Â Â sale_orders = self.env['sale.order'].search(domain=[('partner_id', 'in', self.ids)]) Â Â Â Â for partner in self: Â ...