Adding models

Models define the data structures to be used by our business applications. This recipe shows how to add a basic model to a module.

We will use a simple book library example to explain this; we want a model to represent books. Each book has a name and a list of authors.

Getting ready

We should have a module to work with. If we follow the first recipe in this chapter, we will have an empty my_module. We will use that for our explanation.

How to do it…

To add a new Model, we add a Python file describing it and then upgrade the addon module (or install it, if it was not already done). The paths used are relative to our addon module location (for example, ~/odoo-dev/local-addons/my_module/):

  1. Add a Python file to the module, models/library_book.py, with the following code:
    # -*- coding: utf-8 -*-
    from openerp import models, fields
    class LibraryBook(models.Model):
        _name = 'library.book'
        name = fields.Char('Title', required=True)
        date_release = fields.Date('Release Date')author_ids = fields.Many2many('res.partner', string='Authors')
  2. Add a Python initialization file with code files to be loaded by the module models/__init__.py, with the following code:
    from . import library_book
  3. Edit the module Python initialization file to have the models/ directory loaded by the module:
    from . import models
  4. Upgrade the Odoo module, either from the command line or from the apps menu in the user interface. If you look closely at the server log while upgrading the module, you should see this line:
    openerp.modules.module: module my_module: creating or updating database tables

After this, the new library.book model should be available in our Odoo instance. If we have the technical tools activated, we can confirm that by looking it up at Settings | Technical | Database St ructure | Models.

How it works…

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

Odoo models are objects derived from the Odoo Model Python class.

When a new model is defined, it is also added to a central model registry. This makes it easier for other modules to later make modifications to it.

Models have a few generic attributes prefixed with an underscore. The most important one is _name, providing a unique internal identifier to be used throughout the Odoo instance.

The model fields are defined as class attributes. We began 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 from other models.

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

There's much more to say about models, and they will be covered in more depth in Chapter 4, 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 existing methods, such as create() or write(). This is addressed in Chapter 5, 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