To extract grouped results, we will add the _get_average_cost method, which will use the read_group() method to fetch the data in a group:
@api.model
def _get_average_cost(self):
grouped_result = self.read_group(
[('cost_price', "!=", False)], # Domain
['category_id', 'cost_price:avg'], # Fields to access
['category_id'] # group_by
)
return grouped_result
To test this implementation, you need to add a button in the user interface that triggers this method.