many2many-fields-and-its-widgets-odoo.png
Blogger_636316573193522676.jpg
By: Treesa

Many2Many Fields and Its Widgets in Odoo

Technical

Odoo is an open-source business software that supports a wide range of business domains. A many2many widget is a data type in Python that helps to build Odoo.In this blog, we will talk about the widgets that support the many2many field. This is a technical blog that helps Odoo developers to set their preferred view for a many2many field.

 


There are some many2many widgets in Odoo as follows:

 

- many2many widget (default)

- many2many_tags widget

- many2many_checkboxes  widget

- many2many_kanban widget

- many2many_binary widget

- x2many_counter widget


1. many2many Widget (default)


This widget is used in default cases.Many2many widget generates a list view of the related model and the objects in the model.

 

Example Code :

 

.py     - field_name =fields.Many2many('res.partner',string="many2many_default").xml   -  <field name="field_name" />

 

Here is the view for this :

many2many-fields-and-its-widgets-odoo-cybrosys

We can add options like :

                       

.xml - <field name="field_name" options="{'no_create': True}"/>

 

no_create: This will remove “Create” button. That is to model as we map the objects from the model but the creation of data not possible



2. many2many_tags widget


This widget allows multiple selections of items. This method is implemented in a project module for adding “tags”.

 

Example Code :

 

.py     -  field_name = fields.Many2many('res.partner', string="many2many_tags").xml   -  <field name="field_name" widget="many2many_tags"/>

 

Here is the view for this :


many2many-fields-and-its-widgets-odoo-cybrosys

We can add options like :

                       

.xml -  <field name="field_name"	     widget="many2many_tags"	       options="{'no_create_edit': True}"/>

 

no_create_edit: Removes the “Create “agro” ” option.

no_quick_create: Removes the “Create and Edit” option.

 

 

3. many2many_checkboxes  widget

 

This type of widget display a list of checkboxes.Many2many_checkboxes display a single checkbox for each record existing in the model depending on the relation and domain. Checked records will be added to the field.

 

Example Code :


.py     -  field_name = fields.Many2many('res.partner',           string="many2many_checkboxes").xml   - <field name="field_name" widget="many2many_checkboxes"/>

 

Here is the view for this :


many2many-fields-and-its-widgets-odoo-cybrosys

4. many2many_kanban widget


In the many2many_kanban widget, related objects are viewed in Kanban View. Depending on the kanban view defined, the look and impact of the widget changes accordingly. This is more comfortable because it can vary its look based on customer requirements by changing the kanban view definition. Here is an example code for the many2many_kanban widget


Example Code :

 

.py - field_name = fields.Many2many('res.partner', string="4") .xml -         <field name="field_name" widget="many2many_kanban">   <kanban quick_create="false" create="true" delete="true">      <field name="name"/>      <templates>          <t t-name="kanban-box">              <div class="oe_kanban_global_click" style="position: relative">                  <a t-if="! read_only_mode" type="delete" style="position: absolute; right: 0; padding: 4px; diplay: inline-block">X</a>                  <div class="oe_module_vignette">                      <img t-att-src="kanban_image('res.users', 'image_small', record.id.value)" class="oe_avatar oe_kanban_avatar_smallbox"/>                      <div class="oe_module_desc">                          <field name="name"/>                      </div>                  </div>              </div>          </t>      </templates>  </kanban></field>

 

Here is the view for this :


many2many-fields-and-its-widgets-odoo-cybrosys


5. many2many_binary widget


Many2many_binary is used for attachment purposes. Attach a file to the email are examples of this. The user can upload as many files and attach this to a model.

This widget works exclusively on many2many fields associated with the ir.attachment model.

 

Example Code :

 

.py -            field_name = fields.Many2many(                      comodel_name="ir.attachment", relation="m2m_ir_attachment_relation",		       column1="m2m_id", column2="attachment_id", string="Attachments",) .xml -          <field name="field_name"		      widget="many2many_binary"	 	      string="Attach a file"/>


Here is the view for this :


many2many-fields-and-its-widgets-odoo-cybrosys

6. x2many_counter widget 

 

A simple, read the only widget displaying a link with information about the number of related items. The link’s target view can be configured with the views option.

Also useful with one2many fields.

The views to display in the act_window action. Must be a list of the tuple whose first element is the id of the view to display (or False to take the default one) and the second element is the type of the view. Defaults to [[false, "tree"], [false, "form"]].

 

Example Code :


.xml  -  <field name="field_name" widget="x2many_counter"> 


If you want to learn on how to Save Many2many Field in Odoo 13 Settings refer our previously published blog.


If you need any assistance in odoo, we are online, please chat with us.


cybrosys youtube

Comments

7

Hari

What about many2many_tags_email?

29/04/2020

-

4:36AM


Niyas Raphy

Hi Drupesh, Are you asking about how to use many2many field in search view or how to display many2many field in kanban view?

23/02/2019

-

2:47AM


Mugia

When i use default many2many widget, how can i remove trash button(to remove row/record) to a certain condition (example: hide trash button when field mandatory is set true)

09/11/2018

-

1:40AM


Jithin

In the many2many widget itself without clicking on add an item do we get a selection box similar to list view?

09/09/2020

-

9:50PM


Arturo Flores

Really usefull page, thanks

06/11/2018

-

11:19AM


Dhrupesh

How can add search bar in many2many field's kanban view ?

06/02/2019

-

6:55AM


Notteccon

Hi Treesa Thanks, very helpfull. Would you give me a a tip about many2many_binary widget in portal (Frontend)? I implemented the controller and the template with all the fields but the Many2many 'ir.attachment' Thanks again

05/10/2020

-

4:33AM




Leave a comment

 
Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message