- 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/
):
- 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')
- 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
- Edit the module Python initialization file to have the
models/
directory loaded by the module:from . import models
- 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.
- JavaScript從入門到精通(微視頻精編版)
- SoapUI Cookbook
- MySQL 8從入門到精通(視頻教學版)
- Python程序設計(第3版)
- 人人都是網站分析師:從分析師的視角理解網站和解讀數據
- 重學Java設計模式
- Serverless架構
- Learning Concurrent Programming in Scala
- HTML5+CSS3 Web前端開發技術(第2版)
- 零基礎學Kotlin之Android項目開發實戰
- INSTANT Adobe Edge Inspect Starter
- 軟件工程基礎與實訓教程
- 微信小程序開發實戰:設計·運營·變現(圖解案例版)
- 寫給大家看的Midjourney設計書
- Android嵌入式系統程序開發(基于Cortex-A8)