How it works...

Our first step was to create a Python file where our new module was created.

The Odoo framework has its own ORM framework. This ORM framework provides abstraction over the PostgreSQL database. By inheriting the Odoo Python class Model, we can create our own model (table). When a new model is defined, it is also added to a central model registry. This makes it easier for other modules to make modifications to it later.

Models have a few generic attributes prefixed with an underscore. The most important one is _name, which provides a unique internal identifier that will be used throughout the Odoo instance. The ORM framework will generate the database table based on this attribute. In our recipe, we used _name = 'library.book'. Based on this attribute, the ORM framework will create a new table called library_book. Note that the ORM framework will create a table name by replacing . with _ in the value of the _name attribute.

The model fields are defined as class attributes. We began by defining the name field of the Char type. It is convenient for models to have this field because, by default, it is used as the record description when referenced by other models.

We also used an example of a relational field, author_ids. This defines a many-to-many relation between Library Books and its partners. A book can have many authors and each author can have written many books.

There's much more to say about models, and they will be covered in depth in Chapter 5, Application Models.

Next, we must make our module aware of this new Python file. This is done by the __init__.py files. Since we placed the code inside the models/ subdirectory, we need the previous __init__ file to import that directory, which should in turn contain another __init__ file, importing each of the code files there (just one, in our case).

Changes to Odoo models are activated by upgrading the module. The Odoo server will handle the translation of the model class into database structure changes.

Although no example is provided here, business logic can also be added to these Python files, either by adding new methods to the model's class, or by extending the existing methods, such as create() or write(). This is addressed in Chapter 6, Basic Server-Side Development.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset