Relational Fields in Odoo

Relational Fields in Odoo

Relational Fields in odoo are needed to connect one model to the other models. Relational Fields are used to connect among models. Usually, it is used for developing and implementing in odoo with creating the models as needed.

For example, in the Book Borrowing model, there are fields related to the List of Books and User models.

This is three types of Relational fields in Odoo:

1. Many2one field

2. One2many field

3. Many2many field

1.    Many2one fields

The Many2one field is used to connect one model with several available model records, for the second model is usually called a co-model. Many2one has the id ending the feature.

For example for defining a Many2one field in the model like:

book = fields.Many2one(string='Book', comodel_name='library_basic.library_basic')

Don’t forget to add the ’book’ to the view.xml 

<field name="book"/>

After everything is done then the model form will look like

The contents of the field book are obtained from another record.

2.    One2many fields

One2Many fields are the opposite of Many2One fields. This field is linked to one record of the various available models. To connect it takes 2 things, such as:

  1. 'co-model name'. Contains the required model name
  2. 'inverse name'. Contains the opposite name of the co-model. One2Many has ids ending.

You can define an One2many field in the model as:

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

Example parameters that can be used for the field are:

a) comodel_name (str) – the name of the target model that creates the relational field.

b) inverse_name (str) – This is the inverse name of Many2one field name in comodel_name.

c) domain – To provide a field domain to filter the records with conditions.

d) context (dict) – To provide context which use on the client side when handling that field.

e) auto_join (bool) – To decide if a JOINs is returned after a search through the field (default: False)

f) limit (int) – To provide limits for use while reading.

3.    Many2many fields

Many2Many fields are a two-way relationship. This means that Many2many fields can connect one side of the model to the other side of the model.

The critical parameter of this field is - 'comodel_name', which is the name of the associated model. By convention, the Many2many field ends in _ids.

You can define a Many2many field in the model as:

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

For example, if want to relate a model with ‘res.users’ model and give the name to the field as ‘Users’,  then define a Many2many field called ‘user_ids’ as:

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

If you want to see the full code, you can visit the following page.

Conclusion

Odoo has many models so it is not uncommon for there to be a relationship between two models or more than that the function of this relation is very important because it can connect from one model or field to another field model and the customization is not too complex.

Relational
Fields in Odoo
Alfin alfalah 30 November, 2022
Share this post
Archive
Sign in to leave a comment
Understanding What RFQ is and How To Make It