Relational Fields in OpenERP
Connect With Us
Odoo gives you the ability to link or relate records in a "Parent/Child" hierarchy. This is done with link to parent/child record fields which we also call relational fields. Mainly there are 3 relational fields:
-
many2one (Relationship towards a parent object (using a foreign key))
-
one2many (Virtual relationship towards multiple objects (inverse of many2one))
-
many2many (Bidirectional multiple relationship between objects)
-
related (Shortcut field equivalent to browsing chained fields)
many2one:
This type of field associates this object to a parent object via this Field. For example "Product Category" "Product" belongs to would Many to one. i.e Many Products will belong to a Product Category. Syntax:fields.Many2one('object.name', string='Field Name', optional parameters) Where:
-
object.name: _name of destination object (required),
-
Field Name: String/Label of the field,
-
Optional parameters:
ondelete: What should happen when the resource this field points to is deleted. Predefined values are "cascade", "set null", "restrict", "no action", "set default". Default value is "set null".
For more information about ondelete action refer: Referential Actions
required: True (Makes field mendatory) readonly: True (Won't allow to edit) index: True - (Creates an index on the Foreign Key field)
-
Example: category_id= fields.Many2one('product.category', string='Category')
IMAGE OF Category Scroll Down box
Delete You can find source code (class) for many2one field in:
-
v10.0,11.0,12.0: odoo/fields.py: class Many2one(_Relational)
one2many:
This field provides a virtual relationship towards multiple objects (inverse of many2one). Syntax:fields.One2many('object.name', 'field_id(m2o)', string='Field name', optional parameter) Where:
-
object.name: _name of destination object (required)
-
field_id: field name of inverse many2one, i.e. corresponding foreign key (required)
-
Optional parameters:
invisible: True/False states: ? readonly: True/False
-
Example: I want to display how many products are there which belong to a particular category. Then I will use one2many field in Product Category. Product_ids = fields.One2many('product.product', 'category_id', string='Products')
Delete It is mandatory to define many2one field in the destination object (in our case product.product). You can find the source code (class) for one2many field in:
-
V10.0,11.0,12.0: odoo/fields.py: class One2many(_RelationalMulti):
many2many:
Encapsulates the logic of a many-to-many bidirectional relationship, handling the low-level details of the intermediary relationship table transparently. A many-to-many relationship is always symmetrical, and can be declared and accessed from either endpoint model. Syntax: fields.Many2many('object.name', 'relation object (table name)', 'current.object.id', 'other.object.id', string='Field Name') Where:
-
object.name: _name of destination object (required)
-
relation object: relationship table to use,Since v6.1 it has been become an optional name of the intermediary relationship table. If not specified, a canonical name will be derived based on the alphabetically-ordered model names of the source and destination (in the form: ``amodel_bmodel_rel``). Automatic naming is not possible when the source and destination are the same, for obvious ambiguity reasons.
-
current.object.id: name of field in rel table storing the id of the current object since v6.1it has been become an optional name for the column holding the foreign key to the current model in the relationship table. If not specified, a canonical name will be derived based on the model name (in the form: `src_model_id`).
-
other.object.id: name of field in rel table storing the id of the target object. since v6.1 it has been become an optional name for the column holding the foreign key to the destination model in the relationship table. If not specified, a canonical name will be derived based on the model name (in the form: `dest_model_id`)
-
Optional parameters:
invisible: True/False states: ? readonly: True/False
-
Example: category_id = fields.Many2many('res.partner.category', 'partner_categ_rel', 'partner_id', 'category_id', string='Tags')
Delete You can find the source code (class) for many2many field in:
-
V10.0,11.0,12.0: odoo/fields.py: class Many2many(_RelationalMulti):
related:
Field that points to some data inside another field of the current record. Sometimes you need to refer to the relation of a relation. For example, suppose you have objects: City -> State -> Country, and you need to refer to the Country from a City, you can define a field as below in the City object:
State_id = fields.Many2one('res.country.state', string='State')
Country_id = fields.Many2one("res.country", related=’state_id.country_id', string="Country")
Where:
-
The first parameter is the reference table name to look up that reference in.
-
Use related is the chain of reference fields to follow, with the desired field at the end.
Become an Odoo Champ

Hi, I would like to return many2one not by _name or _rec_name. I have 3 classes for the following: class LecturerWorkday(models.Model): _name = 'lecturer.workday' _rec_name = 'lecturer_id' name = fields.Selection([('sunday','Sunday'),('monday','Monday'),('tuesday','Tuesday'), ('wednesday','Wednesday'),('thursday','Thursday'),('friday','Friday'),('saturday','Saturday'), ], default='sunday',string="Workday", required=True) lecturer_id = fields.Many2one('school.lecturer', string="Lecturer Name", invisible=True) class SchoolLecturer(models.Model): _name = 'school.lecturer' name = fields.Char(string="Lecturer Name", required=True) workday_id = fields.Many2one("lecturer.workday", string="Workday ID") class LecturerTimeoff(models.Model): _name = "lecturer.timeoff" lecturer = fields.Many2one('school.lecturer', string="Lecturer Name") day_m2o = fields.Many2one('lecturer.workday', string="Lecturer Workdays") reason = fields.Char("Time off Reason") @api.onchange('lecturer') def get_lecturer_workday(self): day_obj = self.env['lecturer.workday'].search([('lecturer_id', '=', self.lecturer.id)]).mapped('name') day_list = [] for rec in day_obj: day_list.append(rec) res = {} res['domain'] = {'day_m2o': [('name', '=', day_list)]} return res print (res) When I choose lecturer name, day_m2o should display the workday of selected lecturer name. I have been trying to compute it as above, but the result is still display lecturer name, instead of workday. Need your help.