Home Blog Relational Fields And Widgets In Odoo
Relational Fields And Widgets In Odoo

Relational Fields and Widgets In Odoo

Fields are used for storing & maintaining data in Odoo . Fields are like the columns in a table where the information is stored. In Odoo, fields are created using Python code. Through this blog , You will get to know detailed information about the “Relation Fields & Widgets in Odoo”.

We can also use multiple parameters on these field/data types based on our preference.

Mainly there are 3 different types of fields in Odoo: 

1) Basic Fields

2) Computed Fields

3) Relational Fields

1) Basic fields refer to simple fields like Character fields, Integer fields, Binary fields, Text fields, DateTime Fields, and Boolean fields.

Mainly there are 3 different types of fields in Odoo: Basic Fields

2) Computed or Calculated Fields are fields that do not store any data or take input directly from the user. Instead, these fields are calculated based on the inputs of other fields. The calculation is done in Real-Time.

3) Relational Fields are the special type of fields that provide the option to link the data of one model with the data of another model. This helps to create a bridge between two different models and link the data of two different models.

There are three main types of Relational Fields in Odoo:

(a) Many2one Field

(b) One2many Field

(c) Many2many Field


 Subscribe our Newsletter for Odoo tips, technical insights, and more!

 
Let us Discuss these fields in detail...

Many2one Field

A many2one field is used to link the current object to another object which acts as a parent for this object. In simple terms, when we can show the records of another model and link it with the current child model using a many2one relation.

Syntax for writing a Many2one Field:

field_name = fields.Many2one(comodel name=’model.model (parent model) ’, string=’Field Name’)

Here, as we can see we write the parent model and the name/string which is the name of the field to be displayed.


For example, here we have a custom model, we’’ll try to link with the ‘res.partner’ model as a parent model. So, we can link the parent model with our custom model.

Many2one Relational Fields in Odoo

Now as we can see, we can create/edit and delete records of the parent model from here. We can restrict this option on the field.

In the xml file, we can write like this :

          <field name="many2onefield" options="{'no_create': True, 'no_create_edit':True}" />


This will restrict the access for the user to create or edit records of the parent model through Many2one field here.

Many2one Relational Fields in Odoo

We can also filter the records shown here in the Many2one field. We can use the domain parameter to filter records based on conditions in the Many2one field.

One2Many Fields

It is a type of relational field and it provides the option to create relation between the child model & multiple rows of the parent model. This type of field can be used in cases where the user wants to store multiple records of the parent model and create a connection between those records and the current record through an inverse Many2one Field.

Syntax for writing an One2many field:

field_name = fields.One2many(comodel name=’model.model (parent model) ’, inverse_name=’name_of_the_inverse_field’, string=’Field Name’)

Here, comodel name (name of the parent model) and the inverse field name are mandatory parameters for creating an One2many relation.


The inverse field name is a Many2one field which should be present in the parent model. The comodel for this Many2one field is the current model. This inverse Many2one field is the key to linking the relationship between the records.

For example, in the custom model, we can create an One2many field like this

One2Many Relational Fields in Odoo

Which we can see in the form view like this:

One2Many Relational Fields in Odoo

Many2many Fields

This type of field is used to create bidirectional multiple relationships between two different models.

This field can be used to create a table between two models. Any record on one side of the table can access any number of records on the other side of the table.

Syntax for writing Many2many field:

field_name = fields.Many2many(comodel name=’model.model’ ,  'relation between objects', ‘column1’, ‘column2’, string=’Field Name’)

The comodel name is the mandatory parameter here.

For example, we can create a Many2many field like this:

Many2many Relational Fields in Odoo

Which we can then display in form like this:

Many2many Relational Fields in Odoo

We can use multiple widgets according to our preference in this Many2many field. Let us see in detail how we can use these widgets in Odoo.

1) Many2many Tags

When this widget is applied, the records in Many2many fields are displayed as Tags.

The user can select multiple tags just like multiple records are selected.

Syntax for using widget:

<field name=”field_name” widget="many2many_tags"/>

Many2many Tags in Odoo

2) Many2many Selection/Checkbox

This widget can be used to create a selection type view of the many2many field. The user can select the record from the field by selecting the checkbox True.

Syntax for using widget:

<field name=”field_name” widget="many2many_checkboxes"/>

Many2many Selection/Checkbox in Odoo

3) One2many List Widget

This widget will be used to view Many2many field like a One2many field.

Syntax for using widget:

<field name=”field_name” widget="one2many_list"/>

 

4) Many2many Binary Widget

This type of widget is used only when the field is Many2many with the ‘ir.attachment’ model of Odoo. This widget is used to create an attachment in the model.

Syntax for using widget:

<field name=”field_name” widget="many2many_binary"/>

Many2many Binary Widget in Odoo

We can also use various parameters in the relational fields. For example, Use ‘Domain’ in case you want to filter records from a Many2many field. Or in simple words, if you want to display only those records which fulfill certain conditions.

You can also use Kanak Custom Fields Demo Module by Kanak Infosystems, which contains simple and Relational Fields used in Odoo With No additional Configuration required. You can contact our Odoo Developers to get more assistance.

I hope now you can understand "Relational Fields and Widgets In Odoo". Share your thoughts about the difficulties that you have faced while creating these fields. Also, If you have faced any difficulties to create these fields, do let us know in the comment section.

Book a Free Odoo Consultation

Get In Touch with Us

Leave a Comment

Your email address will not be published.

Submit
Your comment is under review by our moderation team.