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, orm2oforone-to-many, oro2mfor shortmany-to-many, orm2mfor 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
m2ofield for the hostel inHostel Room:class HostelRoom(models.Model): Â Â Â Â # ... Â Â Â Â ...