Exposing related fields stored in other models
Odoo clients can only read data from the server for the fields that belong to the model they are querying. They cannot access data from related tables using dot notation as server-side code can.
However, we can make the data from related tables available to the clients by adding it as related fields. This is what we will do to get the hostel of the room in the student model.
Getting ready
We will continue using the my_hostel
add-on module from the previous recipe.
How to do it...
Edit the models/hostel_student.py
file to add the new related
field.
Ensure that we have a field for the hostel room, and then, we add a new relation field to link the student with their hostel:
class HostelStudent(models.Model): _name = "hostel.student" # ... hostel_id = fields.Many2one("hostel.hostel", related='room_id.hostel_id')
Finally...