官术网_书友最值得收藏!

  • Odoo Development Cookbook
  • Holger Brunn Alexandre Fayolle Daniel Reis
  • 603字
  • 2021-07-16 11:00:32

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.

主站蜘蛛池模板: 修武县| 扎鲁特旗| 卢氏县| 卓资县| 巴中市| 思茅市| 瑞安市| 耿马| 铅山县| 疏勒县| 磐石市| 治多县| 家居| 肥乡县| 濮阳县| 安福县| 甘谷县| 清涧县| 阿坝| 鄂尔多斯市| 仙桃市| 简阳市| 四川省| 丰都县| 隆昌县| 始兴县| 新郑市| 荥阳市| 共和县| 南宁市| 大邑县| 天长市| 西畴县| 闵行区| 莫力| 施甸县| 杭州市| 辽宁省| 临西县| 龙山县| 宣城市|