Adding a hierarchy to a model
Hierarchies are represented like a model having relations with the same model. Each record has a parent record in the same model, and many child records. This can be achieved by simply using many-to-one relations between the model and itself.
However, Odoo also provides improved support for this type of field by using the nested set model (https://en.wikipedia.org/wiki/Nested_set_model). When activated, queries using the child_of
operator in their domain filters will run significantly faster.
Staying with the Library Books
example, we will build a hierarchical category tree that can be used to categorize books.
Getting ready
We will continue using the my_library
add-on module from the previous recipe.
How to do it...
We will add a new Python file, models/library_book_categ.py
, for the category tree, as follows:
- To load the new Python code file, add the following line to
models/__init__.py
:from . import library_book_categ
- To...