Creating a new model
Models are the basic components for building applications and provide the data structures and storage to be used. Next, we will create the model for our to-do list app with three fields:
- Description text
- The
Is Done?
flag - Work team (that is, a list of people collaborating in this item)
Model names should use the singular form, so the new model should be named To-do Item. The model technical name must be a valid database object identifier, so we should use letters and underscores and avoid other symbols. Since the models created through the Technical menu must have an x_
prefix, the technical name for the new model will be x_todo_item
.
Model definitions are accessed in the Settings app in the Technical | Database Structure | Models menu.
To create a new model, click the Create button on the Models list:
- Fill in the basic definition values for it – enter
To-do Item
in the Model Description field andx_todo_item
for the Model field. - By default, the model will include in the fields list the
x_name
field. This is a title that represents the record in lists or when it is referenced in other records. It can be used for the To-do Item title, so edit it to change the Field Label column accordingly. - Next, add the
Is Done?
field. This should be straightforward. On the Fields list, click Add a line at the bottom of the list to open the new field form, and then, enter these values:- Field Name:
x_is_done
- Field Label:
Is Done?
- Field Type: boolean
Then, click the Save & Close button and click Save on the model form.
- Field Name:
- Now, adding the Work Team field should be a little more challenging. Not only is this a relation field that refers to records in the Contact (
res.partner
) model, but it is also a multiple-value selection field.Fortunately, Odoo supports many-to-many relations. This is the case here since a to-do item can be related to many contacts, and each contact can be related to many to-do items.
To add the Work Team field on the Fields list, click again on the form Edit button, then click Add a line to open the new field form. Then, enter these values:
- Field Name:
x_work_team_ids
- Field Label:
Work Team
- Field Type:
many2many
- Related Model:
res.partner
- Domain:
[('x_is_work_team', '=', True)]
Many-to-many fields have a few specific base properties: Relation Table, Column 1, and Column 2. These are automatically filled out for you, and the defaults work for most cases. These properties are discussed in more detail in Chapter 6, Models – Structuring the Application Data.
The
Domain
attribute is optional and defines a filter for the records to be presented. We are using it to limit the selectable contacts to the ones that have theIs Work Team?
flag checked on them. Otherwise, all contacts would be available for selection.The domain expression to use 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.
Tip
Odoo has an interactive domain filter wizard that can be used as a helper to generate domain expressions. To use it, select the Settings | Technical | User Interface | User-defined Filters menu option. Once a target model is selected in the form, the Domain field will display an + Add filter button to add filter conditions. When doing so, the textbox below it will dynamically show the corresponding domain expression code.
- Field Name:
- When we are done, click the model form Save button. When the new model is created, a few fields are automatically added. The ORM engine includes them in all models, and they can be useful for audit purposes:
We now have the underlying model for the to-do list app, but it is still not accessible by users. For that, access security needs to be configured. So, let's look at that in the next section.