Skip to Content

Group by for Many2Many Fields in Odoo 17: A Technical Guide

In Odoo development, Many2Many fields are crucial for establishing complex relationships between records across different models. However, grouping records based on Many2Many fields can be challenging. This guide walks you through how to implement grouping for Many2Many fields in Odoo 17 effectively.



Understanding Many2Many Fields

Many2Many fields allow records in one model to relate to multiple records in another model (or the same model). This relationship is bidirectional, meaning that changes in one record can reflect in all related records. In contrast, Many2One fields create a simpler, one-way relationship.


Step-by-Step Implementation

Let’s consider a scenario where you have a sales order model with a Many2Many field called tag_ids, which stores multiple tags associated with each order. The goal is to enable grouping of sales orders based on these tags.


1. Define a Computed Field

First, define a computed field that will aggregate tag names for each sales order. This field will be used for grouping.

product_tags = fields.Char(string='Tags', compute='_compute_product_tags', store=True)


2. Implement the Computed Method

Next, create a method that populates the product_tags field with a comma-separated string of tag names.

@api.model
@api.depends('tag_ids')
def _compute_product_tags(self):
    for rec in self:
        rec.product_tags = ','.join([tag.name for tag in rec.tag_ids]) if rec.tag_ids else ''

This method aggregates the tag names and assigns them to the product_tags field.


3. Update the View

Finally, update the corresponding XML view to include the product_tags field and enable Group By functionality.

<odoo>
   <record id="view_order_form" model="ir.ui.view">
       <field name="name">sale.order.view.form.inherit.custom_module</field>
       <field name="model">sale.order</field>
       <field name="inherit_id" ref="sale.view_order_form"/>
       <field name="arch" type="xml">
           <xpath expr="//field[@name='payment_term_id']" position="after">
               <field name="product_tags"/>
           </xpath>
       </field>
   </record>

   <record id="view_sales_order_filter" model="ir.ui.view">
       <field name="name">sale.order.view.list.inherit.custom_module</field>
       <field name="model">sale.order</field>
       <field name="inherit_id" ref="sale.view_sales_order_filter"/>
       <field name="arch" type="xml">
           <xpath expr="//search/group/filter[@name='customer']" position="after">
               <separator/>
               <filter name="tag_ids" string="Tags" domain="[]" context="{'group_by': 'product_tags'}"/>
           </xpath>
       </field>
   </record>
</odoo>

This XML code customizes the sales order view, adding the computed field and enabling grouping by tags.


Conclusion

By implementing computed fields, writing efficient methods, and updating views, you can effectively group records by Many2Many fields in Odoo 17. This approach not only organizes data more effectively but also provides better insights into the relationships between records.