+91 9818211679, +91 9998040067       [email protected]

Relational Fields & 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 the 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 field in Odoo

1) Basic Fields

2) Computed Fields

3) Relational Fields

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

Relational Fields & Many2many Widgets In Odoo

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

 

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.

Relational Fields & Many2many Widgets 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.

Relational Fields & Many2many Widgets 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 case where the user want 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 a 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 a 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 in linking the relation between the records.

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

Relational Fields & Many2many Widgets In Odoo

Which we can see in the form view like this:

Relational Fields & Many2many Widgets 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:

Relational Fields & Many2many Widgets In Odoo

Which we can then display in form like this:

Relational Fields & Many2many Widgets 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"/>

Relational Fields & Many2many Widgets 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"/>

Relational Fields & Many2many Widgets 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"/>

Relational Fields & Many2many Widgets 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 used Kanak Custom Fields Demo Module by Kanak Infosystems which contain 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 about "Relational Fields and Widgets In Odoo". Share your thoughts about the the difficulties that you have faced while creating this fields. Also If you have facing any difficulties to create this fields do let us know in the comment section.

There are no comments for now.

There are no comments for now.


There are no comments for now.