Do You know Hidden Risks of Poor ERP Implementation? Download Free E-Book

Relational fields in Odoo

September 15, 2013 by
Relational fields in Odoo

Relational fields are used to link the record of one model with the record of another model(s).

Different types of relational fields in odoo:

  1. Many2one
  2. One2many
  3. Many2many

 

1. Many2One Field in odoo

Many2one field can be used to link the record of the current model with another model, called co-model.

It can be defined as:

field_id = fields.Many2one(comodel_name=‘comodel.name’, optional_parameters)

For example, we can say the relation between a sale order and a customer as many2one

Parameters:

a) comodel_name (str):- Name of the target model (mandatory), except for related or extended fields.

b) domain:- An optional domain to set on the client side.

c) context (dict):-  An optional context to use when handling the field.

d) on delete (str):- To decide what to do when the referred record is deleted.

The values can be: 'set null', 'restrict', 'cascade'

 

2. One2Many Field

This is the inverse of the many2one relation. This is used to relate one record of the co-model to many records of the model.

It can be defined as:

field_id = fields.One2many(comodel_name=‘comodel.name’, inverse_name=’inverse field name’)

For example, we can say the relation between a customer and sale orders is one2many. A customer can have one or more sale orders.

Parameters:

a) comodel_name (str):- Name of the target model.

b) inverse_name (str):- Name of the inverse many2one field in co-model.

c) domain:- An optional domain to set on the client side.

d) context (dict):-  An optional context to use when handling the field.

 

3. Many2many field

This is used for establishing bi-directional relation between models. Any number of records on one side can be related to any number of records on other side.

A many-to-many relationship is always symmetrical and can be declared and accessed from either endpoint model.

It can be defined as:

field_id = fields.Many2many(‘comodel.name’, ’product_tax_rel’, ‘column_1’, ‘column_2’)

Parameters:

a) comodel_name (str):- Name of the target model (mandatory), except for related or extended fields.

b) relation (str):- Optional name of the table to store the relation in the database.

c) column_1 (str):- Optional name of column referring to the records of these model.

d) column_2 (str):- Optional name of column referring to the records of those model.

e) domain:- An optional domain to set on the client side.

f) context (dict):-  An optional context to use when handling the field.

Attributes relation, column_1, and column_2 are optional. If not given, it will automatically create the names from model names.

 

Commands:

Many2many and One2many fields can be updated by using special commands. Each command is a 3-element tuple, where the first element is the mandatory integer which identifies the command.

The second element can be either the related record id or 0. The third element can be either the values to write on the record, either new ids (list of related records) or 0.

CREATE = 0

UPDATE = 1

DELETE = 2

UNLINK = 3

LINK = 4

CLEAR = 5

SET = 6

 

classmethod create(values)

Create new records in the comodel using the values, and links the created records to self.

Returns the command (CREATE, 0, values)

 

classmethod update(id, values)

Write values on the related record.

Returns the command (UPDATE, ID, 0)

 

classmethod delete(id)

Removes the related record from database and also removes its relation with self.

Returns the command (DELETE, ID, 0)

 

classmethod unlink(id)

Removes the relation between self and related record.

Returns the command (UNLINK, ID, 0)

 

classmethod link(id)

Adds a relation between self and related record.

Returns the command (LINK, ID, 0)

 

classmethod clear()

Removes all records from the relation with self.

Returns the command (CLEAR, 0, 0)

 

classmethod set(ids)

Replace the current relations of self by the given ones.

Returns the command (SET, 0, Ids)

 

Connect with us & Explore More about SerpentCS 

Apps https://bit.ly/SCS_AppsStore

 

 



 

 

 

 

 

 

 

 

in_webinar_image