Odoo 16 Development Book

Relational fields

The relational fields in Odoo are used to link one model to another model. In any real business scenario, we must create relational links between different models to implement and develop our business as per the needs. The three types of relational fields in Odoo are

  • Many2one field
  • One2many field
  • Many2many field

Many2one fields:

To connect the current object to another object, use the Many2one field. It is the parent of this object. It links a record from the current model to a record from the second model, also known as the co-model. The _id suffix appears in many2one fields.

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

For example, if we want to link our model to the 'res.partner' model and call the field 'Contact,' we may create a Many2one field called 'partner_id,' which looks like this:

partner_id = fields.Many2one('res.partner', 'Contact')

If we need to retrieve a contact's name, which is contained in the comodel as a character field called 'name,’ Then we can use partner_id.name

One2many fields:

The inverse of the Many2one relationship is the One2Many field relation. The relationships between the many rows of the parent and the child model can be created using this field. The usual,

One2many fields have the _ids suffix.

The syntax for writing the One2many field:

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

comodel_name: It is the name of a related model.

inverse_name: It is the name of the inverse Many2one field in comodel.

The values of fields behave like a list. Therefore we can access the data using a loop.

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

For example, consider the order_line_ids field defined in the model ‘sale.order’

order_line_ids = fields.One2many('sale.order.line', 'order_id', string='Order Lines')

Here, the ‘order_line_ids’ is the One2many field in the comodel ‘sale.order.line’, which has a Many2one field ‘order_id’, that is related to the ‘sale.order’ model. If you want to get the product of the ‘order_line_ids’, which is stored in the field called ‘product_id’ in the comodel, Then you can use:

for rec in order_line_ids:
	print(rec.product_id.name)

Many2many fields:

Multiple bidirectional relationships between two separate models are created using Many2many fields. Each record on one side can be connected to any number of records on the other.

The _ids suffix is used in many2many fields by convention.

The syntax for writing the Many2many field:

field_ids = field.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.

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, consider the ‘tag_ids’ field defined in the model ‘sale.order.’

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

Here, the ‘tag_ids’ is the Many2many field in the comodel 'crm.tag’, we can find a relational table called ‘sale_order_tag_rel’ in the database, having two columns ‘order_id’ and ‘tag_id.’

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