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

Many2many fields and Its widgets in Odoo

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 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 creation of data not possible



2. many2many_tags widget


This widget allows multiple selections of items. This method is implemented in 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 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 many2many_kanban widget, related objects are viewed in Kanban View. Depending on the kanban view defined, look and impact of the widget changes accordingly. This is more comfortable because it can vary its look based on customer requirement by changing kanban view definition.Here is an example code for 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 purpose.Attach a file in 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 an 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">




Comments

0


Leave a comment