How to Use Group By for Many2Many Fields in Odoo 19
Introduction
Odoo’s ORM offers powerful grouping capabilities through search views and the read_group() API. However, developers often face challenges when attempting to group records using Many2many (M2M) fields.
Unlike Many2one fields, Many2many values are stored in a separate relational table, not directly on the model’s main database table. Because of this structure, Odoo cannot apply SQL GROUP BY operations directly on M2M fields. Attempting to do so may lead to assertion errors or unexpected results.
This document explains a practical and production-safe workaround: creating a stored computed field that represents the M2M values in a simple format, allowing Odoo to group records normally.
Why Native Group By Does Not Work for Many2many
A Many2many field:
Is stored in a relation table
Can contain multiple values per record
Does not exist as a column in the main model table
Odoo’s Group By feature works only with stored columns.Therefore, grouping directly on M2M fields is not supported. Convert the M2M data into a stored computed field (usually Char) and group by that field instead.
In CRM, leads contain a Many2many field called tag_ids (labels), and the goal is to group leads based on their assigned labels.
Define the Computed Field
Extend the CRM Lead model and compute a field based on labels(tag_ids):
How This Works
tag_ids is the Many2many field
mapped('name') fetches label names
set() removes duplicates
sorted() keeps consistent ordering
Empty string avoids NULL values
store=True creates a real database column (required)
This makes label_names usable for Group By.
2. Add Group By Filter
Extend the CRM search view:
Explanation:
a. context="{'group_by': 'label_names'}" tells Odoo to group by the computed field.
b. A blank domain means no extra filtering is applied.
3. Display Labels on Form/Tree View(Optional)
If users should see computed labels:
4. Update the Module and Test
Import model in __init__.py
Add XML files to __manifest__.py
Upgrade module
Open CRM → Leads
Use Search → Group By → Labels
Add the label_names field to the form view and enable grouping by labels.
Once loaded, “Group By Label” will appear under Group By in the Crm Leadsearch panel.
You should now see CRM leads grouped according to the stored label values derived from the Many2many tag_ids field.
Odoo does not support grouping by Many2many fields directly because their values are stored in a separate relational table rather than the main model table. By introducing a stored computed field that converts the Many2many data into a database column, you can use Odoo’s built-in Group By functionality. This approach is flexible and can be applied to scenarios such as skills in HR, product categories in Inventory, or service types in project management.
If you are looking for an ERP implementation partner with diverse industry experience feel free to contact us. We have proven track record of successful implementations across various sectors including Odoo for Manufacturing, Odoo for Trading, Odoo for FMCG, Odoo for Oil & Gas, Odoo for Diary, Odoo for Pharma, Odoo for Cosmetic Clinic, Odoo for Contracting Companies, Odoo for HVAC, Odoo for Logistics, Odoo for Automobile, Odoo for Laundry, Odoo for Field Service, Odoo for E-Commerce & many more
ZestyBeanz offers Developer / Consultant outsourcing programs, Chat with us in Whatsapp and Hire Odoo Developers, Mobile Application Developers, Consultants.
#OdooDevelopment #Odoo18 #CustomChatterButton #TechnicalBlog #OdooCustomization #OdooTips #Odoofeatures