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-leftCreate
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 thenEdit
it again. You can see that a few fields were automatically added. The ORM includes them in allModels
, 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 onAdd 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 onAdd 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.
Note
Odoo has an interactive domain filter wizard that can be used as a helper to generate Domain expressions. You can use it at Settings | User Interface | User-defined Filters
. Once a target Model
is selected in the form, the Domain
field will display an add filter button, which can be used to add filter conditions, and the text box below it will dynamically show the corresponding Domain expression code.
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.