- Odoo 12 Development Essentials
- Daniel Reis
- 619字
- 2021-07-02 14:18:38
Creating a new Model
Models are the basic components for applications, providing the data structures and storage to be used. Next, we will create the Model for the To-do Items. It will have three fields:
- Description
- Is done? flag
- Work team partner list
As we have seen earlier, Model definitions are accessed in the Settings app, in the Technical | Database Structure | Models menu.
To create a Model, follow these steps:
- Visit the Models menu, and click on the upper-left Create button. Fill in the new Model form with these values:
-
- Model Description: To-do Item
- Model: x_todo_item
We should save it before we can properly add new fields to it.
- So, click on Save and then Edit it again. You can see that a few fields were automatically added. The ORM includes them in all Models, and they can be useful for audit purposes:

The x_name (or Name) field is a title representing the record in lists or when it is referenced in other records. It makes sense to use it for the To-do Item title. You may edit it and change the Field Label to a more meaningful label description.
Adding the Is Done? flag to the Model should be straightforward now.
- In the Fields list, click on Add a line, at the bottom of the list, to create a new field with these values:
-
- Field Name: x_is_done
- Field Label: Is Done?
- Field Type: boolean
The new Fields form should look like this:

Now, something a little more challenging is to add the Work Team selection. Not only it is a relation field, referring to a record in the res.partner Model, it also is a multiple-value selection field. In many frameworks this is not a trivial task, but fortunately that's not the case in Odoo, because it supports many-to-many relations. This is the case because one to-do can have many people, and each person can participate in many to-do items.
- In the Fields list, click again on Add a line to create the new field:
-
- Field Name: x_work_team_ids
- Field Label: Work Team
- Field Type: many2many
- Object Relation: res.partner
- Domain: [('x_is_work_team', '=', True)]
The many-to-many field has a few specific definitions—Relation Table, Column 1, and Column 2 fields. These are automatically filled out for you and the defaults are good for most cases, so we don't need to worry about them now. These will be discussed in more detail in Chapter 6, Models – Structuring the Application Data.
The domain attribute is optional, but we used it so that only eligible work team members are selectable from the list. Otherwise, all partners would be available for selection.
The Domain expression defines a filter for the records to be presented. It follows an Odoo-specific syntax—it is a list of triplets, where each triplet is a filter condition, indicating the Field Name to filter, the filter operator to use, and the value to filter against. A detailed explanation of domain expressions is given in Chapter 7, Recordsets – Working with Model Data.
We now have the underlying Model for our to-do app, but we still don't have access to it. After creating a Model, we need to configure the groups that can access it. We will do that next.
- 中文版Rhino 5.0實用教程
- Word/Excel/PPT 2010入門與提高(超值版)
- AutoCAD 2016中文版繪圖基礎傻瓜書
- Word Excel PPT商務辦公從新手到高手(白金全彩版)
- MATLAB技術大全
- Office商務辦公實用教程
- 巧學巧用Excel函數:掌握核心技能,秒變數據分析高手
- Excel數據透視表應用之道(雙色板)
- Project 2007企業項目管理實踐
- 精英演示:PPT的商務演示藝術
- 教師必須知道的80個Excel函數
- 文字力!優雅Word的排版與設計實用法則
- Excel數據透視表從新手到高手
- Word/Excel 2010高效辦公從新手到高手(圖解視頻版)
- 辦公自動化高級應用案例教程(微課版)