Skip to Content

Hierarchical Models in Odoo

Guide to Using _parent_store and parent_path

Navigating the intricate world of Odoo, an open-source business management software, can be both challenging and rewarding. One of the key features of Odoo is its support for hierarchical structures, which are indispensable for organizations with complex hierarchies, such as companies with multiple departments or product categories. In this guide, we delve into creating hierarchical models in Odoo using a fundamental concept: the Parent Path Field.

from odoo import models, fields

class MyModel(models.Model):   
​​​_name = 'my.model'   
​_parent_store = True

​​​name = fields.Char(string='Name')   
​​​parent_id = fields.Many2one('my.model', string='Parent')   
​child_ids = fields.One2many('my.model', 'parent_id', string='Children')
​parent_path = fields.Char(index=True, unaccent=False)

The _parent_store attribute is set to True to enable the computation of the parent_path field. The parent_path field is indexed and unaccented, which allows for faster hierarchical queries on the records of the current model using the child_of and parent_of domain operators.

You can use the child_of and parent_of domain operators in Odoo to perform hierarchical queries on records of a model that has a parent-child relationship

In Odoo, you can use these domain operators in various features, such as search views, action views, and reports.

For example, the code snippet below is a method, get_children_department_ids of the hr.department model that returns the IDs of all the child departments of the current department. It uses the search method with the child_of domain operator to find all the child departments of the current department:


def get_children_department_ids(self):
​​return self.env['hr.department'].search([('id', 'child_of', self.ids)]) # Here, self.ids is a list of IDs of the current department and its child
departments. The search method with the child_of domain operator returns all
the departments whose parent_id is in the list of IDs.

The parent_of operator is used to select all the parent records of a given record, including the record itself. For example, to select all the parent departments of a given department, you can use the following domain:

def get_parent_department_ids(self):
		​return self.env['hr.department'].search([('id', 'parent_of', self.ids)])


The parent_path field is used in Odoo to store the complete path of the parent record of a given record. This field is used in models that have a hierarchical structure, where each record has a parent record and can have multiple child records.

In the below code snippet, the parent_path field is used to find the warehouse associated with the source and destination locations of a stock move. The parent_path field of the stock.location model is used to find the warehouse associated with the location. The LEFT JOIN with the stock.warehouse model is performed using the parent_path field of the stock.location model.


LEFT JOIN stock_warehouse whs ON ls.parent_path like concat('%/', whs.view_location_id, '/%')
LEFT JOIN stock_warehouse whd ON ld.parent_path like concat('%/', whd.view_location_id, '/%')


Here, the parent_path field of the stock.location model is used to find the warehouse associated with the source location (whs) and the destination location (whd) of the stock move. The LIKE operator is used to match the parent_path of the location with the view_location_id of the warehouse.

Below are some more examples of how the parent_path field used in Odoo:

Category hierarchy: In the product.category model, the parent_path field is used to store the complete path of the parent category of a given category.

Employee hierarchy: In the hr.employee model, the parent_id and parent_path fields are used to store the manager of an employee and the complete path of the manager's department.

Location hierarchy: In the stock.location model, the parent_id and parent_path fields are used to store the parent location of a given location and the complete path of the parent location.

Chart of accounts hierarchy: In the account.account model, the parent_id and parent_path fields are used to store the parent account of a given account and the complete path of the parent account.

To perform a hierarchical query using the parent_path field, you can use the LIKE operator to match the parent_path of the record with the parent_path of the parent record. For example, to find all the child records of a given record, you can use the following domain:

[('parent_path', 'like', parent_record.parent_path + '/%')]

where parent_record is the parent record of the child records you want to select.

To find all the parent records of a given record, you can use the following domain:

[('parent_path', 'like', '%/' + record.parent_path)]




If you require any Odoo-related services, support, training, development, consultation etc, feel free to contact Hynsys Technologies for excellent support.


 

How to Debug Qweb Using IPDB in Odoo