Fetching data in groups using read_group()
In the previous recipes, we saw how we can search and fetch data from the database. But sometimes, you want results by aggregating records, such as the average cost of last month's sales order. Usually, we use group by
and the aggregate
function in SQL queries for such a result. Luckily, in Odoo, we have the read_group()
method. In this recipe, you will learn how to use the read_group()
method to get the aggregate result.
Getting ready
In this recipe, we will use the my_library
add-on module from Chapter 3, Creating Odoo Add-On Modules.
Modify the library.book
model, as shown in the following model definition:
class LibraryBook(models.Model): _name = 'library.book' name = fields.Char('Title', required=True) date_release = fields.Date('Release Date') pages = fields.Integer('Number of Pages&apos...