Enable Dark Mode!
an-overview-of-relational-fields-in-odoo-19.jpg
By: HIBA N

An Overview of Relational Fields in Odoo 19

Technical Odoo 19 Fields and widgets

Relational fields in Odoo are used to establish connections between different models. They play a crucial role since creating relationships between models is often necessary to design and implement business processes effectively within Odoo.

In this blog, we will explore the various types of relational fields in Odoo.

These fields serve as the key mechanism that enables seamless integration between different models, making them one of the most important and powerful features in Odoo.

The three types of Relational fields in Odoo are:

  1. Many2one field
  2. One2many field
  3. Many2many field

Many2one Fields

A Many2one field creates a relationship where a record in the current model is linked to a single record in another model, known as the co-model. By convention, Many2one field names typically end with the suffix _id.

You can define a Many2one field in your model using the following syntax:

field_id  = fields.Many2one(‘comodel.name’, ‘Field Name’)

The value of a Many2one field can either be empty (no record) or contain a single record. In the database, the column representing this field is of type integer and stores the ID of the related record from the co-model.

You can also easily access the fields of the related record using the syntax:

field_id.name

For instance, if you want to link your model with the ‘product.product’ model and name the field ‘Product,’ you can define a Many2one field as follows:

product_id = fields.Many2one('product.template', string='Product')

To retrieve the product name, which is stored as a character field ‘name’ in the co-model, you can use:

product_id.name

Similarly, to get the product category, defined as a Many2one field ‘categ_id’ in the co-model, you can use:

product_id.categ_id

Parameters used in a Many2one field:

  • comodel_name (str): The name of the target model. This is mandatory unless the field is related or extended.
  • domain: Defines conditions to filter the selectable records.
  • context (dict): Specifies the context to be used on the client side when handling the field.
  • ondelete (str): Determines what happens when the linked record is deleted. Possible values are 'set null', 'restrict', and 'cascade'.
  • auto_join (bool): Indicates whether SQL JOINs should be automatically generated when searching through the field (default is False).
  • delegate (bool): When set to True, the fields of the target model become accessible from the current model (corresponds to _inherits).
  • check_company (bool): Marks the field for verification in the _check_company() method and applies a default company domain based on the field attribute

One2many fields

A One2many field represents the inverse of a Many2one relationship. It links a single record in the current model to multiple records in another model (the co-model). The two key parameters of this field are ‘comodel_name’, which specifies the related model, and ‘inverse_name’, which indicates the name of the corresponding Many2one field in the co-model.

By convention, One2many field names typically end with the suffix _ids.

field_ids = fields.One2many('comodel.name', 'inverse_name', 'Field Name')

The value of a One2many field is a recordset containing all records from the related model where the inverse_name field matches the current record

For example, take the following field defined in the res.partner model.

sale_order_ids = fields.One2many('sale.order', 'partner_id', 'Sales Order')

In this case, sale_order_ids is a One2many field linked to the sale.order model, which contains the Many2one field partner_id pointing back to res.partner. You won’t find a column named sale_order_ids in the res.partner table. Instead, the relationship is stored on the sale.order side, where the partner_id column holds the ID of the related partner.

The parameters used for defining this relational field are:

  • comodel_name (str): Specifies the target model for the relational field.
  • inverse_name (str): Indicates the corresponding Many2one field defined on the target model.
  • domain: Defines conditions to filter the selectable records.
  • context (dict): Provides additional context to be used on the client side when handling this field.

The attributes comodel_name and inverse_name are required, except when the field is a related field or an extension of an existing field.

Man2many fields

A Many2many field represents a bidirectional relationship where multiple records on one side can be linked to multiple records on the other. The key parameter for this field type is comodel_name, which specifies the related model. By convention, Many2many field names typically end with the suffix _ids.

A Many2many field can be defined in your model using the following syntax:

field_ids = fields.Many2many(‘comodel.name’, string =‘Field
Name’)

Since the field holds a list of records, you can access its data by iterating over it, for example:

for rec in self.field_ids:
   print(rec.name)

For example, to create a Many2many link between our model and the res.users model, we can define a field named user_ids. This field will allow selecting multiple users and display the label Users in the interface. The definition looks like this:

user_ids = fields.Many2many('res.users', string='Users', required=True)

In this case, multiple records from a model can be linked to multiple records of the comodel res.users. The following parameters are available when defining such a field:

  • comodel_name – Specifies the target model with which the relationship is created.
  • relation (str) – An optional name for the intermediary database table that stores the relationship.

Another example is the tag_ids field defined in the sale.order model:

tag_ids = fields.Many2many(
   'crm.tag',
   'sale_order_tag_rel',
   'order_id',
   'tag_id',
   string='Tags'
)

When examining the sale.order table in the database, you will not find a column named tag_ids. This is because Many2many fields do not store their values directly in the model’s table. Instead, Odoo creates a separate relational table to manage the association between the two models.

In this case, the relational data is stored in the sale_order_tag_rel table, which contains two columns: order_id and tag_id. These columns hold the corresponding record IDs from the sale.order and crm.tag models, respectively.

To read more about How to Apply Dynamic Domain for Relational Fields in Odoo 18, refer to our blog How to Apply Dynamic Domain for Relational Fields in Odoo 18.


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



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

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

location

Kochi

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

location

Bangalore

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

Send Us A Message