Book Image

Odoo Development Cookbook

By : Holger Brunn, Alexandre Fayolle, Daniel Reis
Book Image

Odoo Development Cookbook

By: Holger Brunn, Alexandre Fayolle, Daniel Reis

Overview of this book

Odoo is a full-featured open source ERP with a focus on extensibility. The flexibility and sustainability of open source is also a key selling point of Odoo. It is built on a powerful framework for rapid application development, both for back-end applications and front-end websites. The book starts by covering Odoo installation and administration, and provides a gentle introduction to application development. It then dives deep into several of the areas that an experienced developer will need to use. You’ll learn implement business logic, adapt the UI, and extend existing features.
Table of Contents (23 chapters)
Odoo Development Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Adding dynamic relations using Reference fields


With relational fields, we need to decide beforehand the relation's target model (or comodel). But 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 to.

With Odoo, this can be achieved using Reference fields.

Getting ready

We will reuse the my_module addon module from Chapter 3, Creating Odoo Modules.

How to do it…

Edit the models/library_book.py file to add the new related field:

  1. We first add a helper method to dynamically build the list of selectable target models:

    from openerp import models, fields, api
    class LibraryBook(models.Model):
        # …
        @api.model
        def _referencable_models(self):
            models = self.env['res.request.link'].search([])
            return [(x.object, x.name) for x in models]
    
  2. Then, we add the Reference field and use the previous function to provide the list of selectable models:

        ref_doc_id = fields.Reference(
            selection='_referencable_models...