Development Book V17: Relational fields

Relational fields in the Odoo framework are utilized to create relationships between different models. You can link and get data from related records by using the associations these variables establish between records of various models. In real business scenarios, establishing relational links between different models is essential for developing and implementing business functionalities according to specific requirements. Odoo offers three types of relational fields to facilitate these connections and data associations. They are

  • Many2many field
  • Many2one field
  • One2many field
Many2one fields:

The Many2one relational field establishes a many-to-one relationship between two models. This field type is used when multiple records from one model are related to a single record in another model. Use the Many2one field to connect the current object to another object. It is this object's parent. It builds a connection between a record from the co-model, or second model, and a record from the present model. Many2one fields have the suffix _id.

field_id = fields.Many2one(‘comodel_name’, ‘Field Name’) comodel_name: It is the name of the related model.

class ProductCategory(models.Model):
    _name = 'product.category'

    name = fields.Char(string='Category Name')

To link the model ‘product.product’ to the ‘product.category' model and use the field Category,' for instance, we could create a Many2one field called category_id,' which would look like this:

class ProductProduct(models.Model):
    _name = 'product.product'

    name = fields.Char(string='Product Name')
    category_id = fields.Many2one('product.category', string='Product Category')

To access a product category’s name stored in a related model's character field named 'name,' you can use category_id.name.

odoo-creating-odoo-modules

Here the ‘Product Category’ field is the Many2one field.

One2many fields:

The One2many field establishes a one-to-many relationship between two models. This field type is used when multiple records from one model are related to a single record in another model. The One2Many field relation is the inverse of the Many2One relationship. This field can be used to create relationships between the several rows of the parent and child models. As usual,

One2many fields have the _ids suffix.

For example,

from odoo import models, fields


class SaleOrder(models.Model):
    _name = 'sale.order'

    name = fields.Char(string='Order Reference')
    order_lines = fields.One2many('sale.order.line', 'order_id', string='Order Lines')
from odoo import models, fields


class SaleOrderLine(models.Model):
    _name = 'sale.order.line'

    name = fields.Char(string='Description')
    quantity = fields.Integer(string='Quantity')
    order_id = fields.Many2one('sale.order', string='Order')

Here the model ‘sale.order’ the ‘order_lines’ field of type One2many that links to the sale.order.line model. The sale.order.line model represents individual lines within an order and includes an order_id field, which is a Many2one field referring back to the sale.order model. The values of ‘order_lines’ behave like a list. Therefore we can access the data using a loop.

def compute_name(self):
    """To get the name of the products"""
    for line in self.order_lines:
        print(line.name)
odoo-creating-odoo-modules

Here is a list of sale order lines within the sales order, presented as a One2many field in the sale order.

Many2many fields:
field_ids = fields.Many2many('comodel_name', 'Field Name')

comodel_name: which is the name of a related model. The values of fields behave like a list. Therefore we can access the data using a loop.

def compute_field(self):
    """To get the field name"""
    for rec in self.field_ids:
        print(rec.name)

Many2many fields have some optional attributes. Optional attributes are ‘relation,’ ‘column_1’, and ‘column_2’.

relation: It stores the relation in the database.

column_1: It is the column referring to our model records in the table ‘relation.’

column_2: It is the column referring to our comodels records in the table ‘relation.’

For example,

from odoo import models, fields


class ProductProduct(models.Model):
    _name = 'product.product'

    name = fields.Char(string='Product Name')
    tag_ids = fields.Many2many('product.tag', 'product_tag_rel',  'product_id', 'tag_id', string='Product Template Tags')
from odoo import models, fields


class ProductTag(models.Model):
    _name = 'product.tag'

    name = fields.Char(string='Tag Name')

The product.product model represents individual products and contains a tag_ids field of type Many2many linking to the product.tag model. The Tag model represents different tags that can be associated with products and includes a product_ids field, also of type Many2many, referring back to the product.product model. Here, the ‘tag_ids’ is the Many2many field in the comodel 'crm.tag’, we can find a relational table called ‘product_tag_rel’ in the database, having two columns ‘product_id’ and ‘tag_id’.

odoo-creating-odoo-modules

These are the tags associated with the product template, accessed through a Many2many field within the product, utilizing the Many2many tags widget.

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