One of the most important uses of the Many2Many field in Odoo development is to allow the records in models to have relationships between the tables. However, developers face trouble when it comes to grouping records based on many2many fields. In this article, we are going to discuss how to use Many2Many fields in Odoo 17 better, mainly by seeing how to effectively use Group By in Many2Many fields.
Understanding Many2Many Fields
But before going to the grouping, you must understand the basics of Many2Many fields. These fields are used to create the relationship between one record to many records of their model or to many records in another model. Unlike Many2Many fields, which set up many-to-many relationships, Many2One fields provide many-to-one relationships, which locks down the model design in a much simpler and more flexible manner in the data model.
Step-by-Step Implementation
Suppose you have a sales order model with a Many2Many field called tag_ids that stores multiple tags associated with each order. Your goal is to enable grouping of sales orders based on these tags.
1. Define the Computed Field
First, you need to define a computed field that will store the aggregated tag names for each sales order. In this example, we'll call it product_tags:
product_tags = fields.Char(string='Tags', compute='_get_tags', store=True)
2. Implement the Computed Method
Next, create a computed method that populates the product_tags field with a comma-separated string of tag names:
@api.model
@api.depends('tag_ids')
def _get_tags(self):
for rec in self:
if rec.tag_ids:
product_tags = ','.join([p.name for p in rec.tag_ids])
else:
product_tags = ''
rec.product_tags = product_tags
3. Update the View
Finally, update the corresponding XML view to include the product_tags field and enable the Group By functionality:
<?xml version="1.0" encoding="utf-8"?>
<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'}"/>
<separator/>
</xpath>
</field>
</record>
</odoo>
With these changes in place, users can now group sales orders by the product_tags field, effectively grouping them based on the associated tags.
In the below screenshot, we can see the groupby tags
Learning the best use case of Many2Many fields and using the Group by feature in Odoo 17 will not only improve your organization of data but also get better insights. With the tips introduced in this extensive guide, you should be well-prepared to break free of the chains of the past and truly harness the power of Many2Many fields in your Odoo development work.
The bottom line is that you need to use computed fields, write efficient computed methods, and update the views accordingly to make grouping and filtering simple. Moreover, integration of OOP concepts, Coding standards, and collaborative programming will make your odoo development skills better and help you to write a base and scalable code for any project.
To read more about How to Use Group by for Many2Many Fields in Odoo 16, refer to our blog How to Use Group by for Many2Many Fields in Odoo 16.