Enable Dark Mode!
what-is-the-difference-between-many2one-vs-one2many-in-odoo-18.jpg
By: Saneen K

What is the Difference Between Many2one vs One2many in Odoo 18

Technical Odoo 18

For linking one model to another model in Odoo, we use the relational fields. These fields help us connect different models. This ensures data consistency and logical connections. In Odoo, there are 3 relational fields used. 

1- Many2one Field

2- One2many Field

3- Many2many Field

In this blog, let's break down the major differences between Many2one and One2Many relational fields.

Many2one Filed

To connect multiple records from one model (called the co-model) to a single record in another model, we use the Many2one relational field.

In the database, a Many2one field adds a foreign key column to the model’s table, which stores the ID of the related record.

Syntax :

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

Example: Consider employees (many) referencing a department (one). Many employees can belong to one department.

department_id = fields.Many2one(‘hr.department’, string=Department)

If we want to access the name of the department from the employee model, we can use ' 

department_id.name’.

Similarly, you can access any field from the ‘hr.department’ model through the department_id.

This adds a dropdown on the form view to select a single ‘hr.department’ record.

The data is stored in the current model’s table.

Parameters used for Many2one relational fields are,

* comodel_name ( str): Name of the target related model. It links to the target model. It is a mandatory attribute and must be a string with the technical name of the model.

* domain: A filter to limit selectable records. Eg: domain="[('customer','=',True)]".

* context:  Passes extra info when selecting or creating a related record. It uses Pre-filling fields when opening a form. Eg :context="{'default_customer': True}"

* ondelete: Defines how the deletion of the related record affects this one.

* Options:

a) ‘cascade’: Delete the dependent record too.

b) ‘set null’: Remove the reference(set to NULL).

c) ‘restrict’:  Prevent deletion if in use.

* auto_join ‘: automatically perform an SQL JOIN with the related model whenever the field is used in a search or read operation.

* delegate: Inherit the fields of the related model and treat them as if they are fields of the current model.

* check_company: It automatically validates that the related record belongs to the same company as the current record.

* default: Sets a default value for the field.

* readonly: Prevents users from changing the value.

* required: Marks the field as mandatory in the UI and for validation. 

* tracking: Enables change tracking (visible in Chatter). Eg: tracking=True

* index: Creates a database index to speed up searches. Eg: index=True

One2many Field

A One2many field is used to define the reverse relationship; one record in the current model can relate to many records in another model. Simply, we can link a record in one model to multiple records in another model.

It's defined as the inverse of a Many2one field and doesn’t create a separate column, instead, it references the field in the related model that connects back. It uses the foreign key defined in the related model.

There are two important parameters of the ‘One2many’ fields, ‘comodel_name’ and ‘inverse_name’. 

comodel_name’  is the name of the related model, and ‘inverse_name’ is the name of the inverse Many2one field in the comodel.

For a One2many field ‘_ids’ suffix while defining the field.

Syntax:

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

Example:

invoice_ids = fields.One2many('account.move', 'partner_id', string='Invoices')

Here, ‘partner_id’ is the Many2one field in the ‘account.move’.

On the form view, this typically displays a list (tree view) of all related records.

Parameters used for a One2many field are:

* comodel_name: Target model for the relation.

* inverse_name: The Many2one field on the target model that links back.

* domain: Limit the related records shown.

* context: Prefill defaults when creating new sub-records. Eg :context="{'default_state': 'draft'}"

For a One2many field, comodel_name and inverse_name are the mandatory attributes.

In Odoo 18, understanding the difference between Many2one and One2many fields is essential for building relational models effectively. Many2one links many records to one, while One2many does the reverse. Choosing the right field depends on the direction of the relationship you need. With these tools, you can structure your data clearly and maintain logical connections across models.

To read more about How to Create One2Many Field in Odoo 17 Website, refer to our blog How to Create One2Many Field in Odoo 17 Website.


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