Adding models
Models define the data structures that will be used by our business applications. This recipe shows you how to add a basic model to a module.
In our example, we want to manage books for a library. To do this, we need to create a model to represent books. Each book will have a name and a list of authors.
Getting ready
We should have a module to work with. If you followed the first recipe in this chapter, Creating and installing a new add-on module, you will have an empty module called my_library
. We will use that for our explanation.
How to do it...
To add a new Model
, we need to add a Python file describing it and then to upgrade the add-on module (or install it, if this was not already done). The paths that are used are relative to our add-on module's location (for example, ~/odoo-dev/local-addons/my_library/
):
- Add a Python file to the
models/library_book.py
module with the following code:from odoo import models, fields class LibraryBook...