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 my_module
addon 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:
- In the
LibraryBook
class, write a method calledget_all_library_members
:
class LibraryBook(models.Model): # ... @api.model def get_all_library_members(self): # ...
- In the body of the method, use the...