Adding dynamic relations using reference fields
With relational fields, we need to decide the relation’s target model (or co-model) beforehand. However, sometimes, we may need to leave that decision to the user and first choose the model we want and then the record we want to link it to.
With Odoo, this can be achieved using reference fields.
Getting ready
We will continue using the my_hostel
add-on module from the previous recipe.
How to do it...
Edit the models/hostel.py
file to add the new related field:
- First, we need to add a helper method to dynamically build a list of selectable target models:
from odoo import models, fields, api class Hostel(models.Model): _name = 'hostel.hostel' # ... @api.model def _referencable_models(self): models = self.env['ir.model'].search([ ...