Adding relational fields to a model
Relational fields are used to represent relations between Odoo models. There are three types of relations:
many-to-one
, orm2o
forone-to-many
, oro2m
for shortmany-to-many
, orm2m
for short
To illustrate this, let’s consider the hostel room model. A room belongs to a single hostel, so the relation between the hostel and the room is m2o
. However, a hostel can have multiple rooms, so the opposite relationship is o2m
.
We can also have a m2m
relationship. For instance, a room can offer various amenities and amenities can be available in different rooms. This is a bidirectional m2m
relationship.
Getting ready
We will continue using the my_hostel
add-on module from the previous recipe.
How to do it...
We will edit the models/hostel_room.py
file to add these fields:
- Add the
m2o
field for the hostel inHostel Room
:class HostelRoom(models.Model): # ... ...