2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Many2many fields are similar to Many2one fields in that they establish a new relationship between models.Odoo 16You cannot group by many2many field because any record can be selected using many2many record. When you give group by filter using many2many field, you will get assertion error.
Introducing how to use Many2Many field groups in Odoo 16。
Here I will use the existing tag_ids many2many field. As shown in the following code:
.py
tag_ids = fields.Many2many('crm.tag', 'sale_order_tag_rel', 'order_id', 'tag_id', string='Tags')
.xml
- <record id="view_sales_order_filter" model="ir.ui.view">
- <field name="name">sale.order.view.list.inherit.module.name</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': 'tag_ids'}"/>
- <separator/>
- </xpath>
- </field>
- </record>
It is possible to use Many2many fields for group_by filtering. This works if we calculate the field, but if the value in the many2many field is 2, it will be displayed with commas (,) like tag1, tag2, etc., because it is a character field.
.py
- product_tags = fields.Char(string='Tags', compute='_get_tags', store=True)
- @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
.xml
- <record id="view_sales_order_filter" model="ir.ui.view">
- <field name="name">sale.order.view.list.inherit.module.name</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>
- <record id="sale_view_form_custom" model="ir.ui.view">
- <field name="name">ale.order.view.form.inherit.module.name</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>
Here we have added a Char field product_tags. It is a calculated field that concatenates the tag name to this field.
You can see that we have provided a grouping filter called Label in the XML file. When we try to group by these sales orders, it will be displayed below.
Similarly, we can add groupby for many2many fields.
Next, let's check how to add filters for many2many fields.
You can create an alternative Many2Many field that computes the value from the many2many field. I'll provide an example to demonstrate this. The field must specify store=True.
.py
- newfield_id = fields.Many2one('example_ids', compute=_compute_newfield_id, store=True)
- @api.depends('example_ids')
- def _compute_newfield_id(self):
- for record in self:
- record.newfield_id = record.example_ids and record.example_ids[0] or False
.xml
<field name="newfield_id" string="Example" filter_domain="[(newfield_id,'ilike', self)]"/>
You can provide the domain name if you wish, as it can be simply searched in the saved fields. You can include the xpath for the location if you wish.
To learn more about creating many-to-many fields in customer portal forms in Odoo 16