How to use grouped()
method in Odoo 18
Enterprise
Enterprise
Introduction
The grouped() method in Odoo is used to group records in a
recordset based on the values of a specific field or set of fields. It
simplifies the process of grouping records and iterating over the
resulting groups, which is especially useful for operations like
reporting, aggregations, or categorizing records.
Let’s discuss the various details of grouped() in Odoo 18 in the
coming slides.
Enterprise
Odoo uses grouped() method with the syntax
Model.grouped(key)
Here, key specifies the field(s) to group by.’
The method returns a dictionary where:
● Keys are the values of the grouping field(s).
● Values are recordsets of the grouped records.
Enterprise
Let’s demonstrate with an example:
Suppose we have the records from the ‘sale.order’ model and we need
to group the record set with the ‘Customer’ field(partner_id).
orders = self.env['sale.order'].search([]) # Fetch all sale orders
grouped_orders = orders.grouped('partner_id')
Here, the variable ‘orders’ contains all the sale orders created so far.
Then, the variable ‘grouped_orders’ contains the grouped data based
on the partner_id field.
Enterprise
grouped() method can be used when
● When you need to categorize or group records based on field
values.
● For performing operations on grouped records, like reporting or
aggregations.
● To simplify and clean up grouping logic in your code.
Always keep in mind that, the fields used in grouped() must be
relational fields or stored fields, as the grouping relies on ORM
lookups. The grouped() method does not support grouping by
computed fields unless they are stored in the database.
For More Info.
Check our company website for related blogs
and Odoo book.
Check our YouTube channel for
functional and technical videos in Odoo.
Enterprise
www.cybrosys.com

How to use grouped() method in Odoo 18 - Odoo Slides

  • 1.
    How to usegrouped() method in Odoo 18 Enterprise
  • 2.
    Enterprise Introduction The grouped() methodin Odoo is used to group records in a recordset based on the values of a specific field or set of fields. It simplifies the process of grouping records and iterating over the resulting groups, which is especially useful for operations like reporting, aggregations, or categorizing records. Let’s discuss the various details of grouped() in Odoo 18 in the coming slides.
  • 3.
    Enterprise Odoo uses grouped()method with the syntax Model.grouped(key) Here, key specifies the field(s) to group by.’ The method returns a dictionary where: ● Keys are the values of the grouping field(s). ● Values are recordsets of the grouped records.
  • 4.
    Enterprise Let’s demonstrate withan example: Suppose we have the records from the ‘sale.order’ model and we need to group the record set with the ‘Customer’ field(partner_id). orders = self.env['sale.order'].search([]) # Fetch all sale orders grouped_orders = orders.grouped('partner_id') Here, the variable ‘orders’ contains all the sale orders created so far. Then, the variable ‘grouped_orders’ contains the grouped data based on the partner_id field.
  • 5.
    Enterprise grouped() method canbe used when ● When you need to categorize or group records based on field values. ● For performing operations on grouped records, like reporting or aggregations. ● To simplify and clean up grouping logic in your code. Always keep in mind that, the fields used in grouped() must be relational fields or stored fields, as the grouping relies on ORM lookups. The grouped() method does not support grouping by computed fields unless they are stored in the database.
  • 6.
    For More Info. Checkour company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com