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

Obtaining an empty recordset for a different model


When writing Odoo code, the methods of the current model are available via self. If you need to work on a different model, it is not possible to directly instantiate the class of that model—you need to get a recordset for that model to start working.

This recipe shows how to get an empty recordset for any model registered in Odoo inside a model method.

Getting ready

This recipe will reuse the setup of the library example in the addon module my_module.

We will write a small method in the library.book model searching for all library.members. To do this, we need to get an empty recordset for library.members.

How to do it…

To get a recordset for library.members in a method of library.book, you need to take the following steps:

  1. In the LibraryBook class, write a method called get_all_library_members:

    class LibraryBook(models.Model):
        # ...
        @api.model
        def get_all_library_members(self):
            # ...
  2. In the body of the method, use the following...