Book Image

Odoo 15 Development Essentials - Fifth Edition

By : Daniel Reis
4.5 (2)
Book Image

Odoo 15 Development Essentials - Fifth Edition

4.5 (2)
By: Daniel Reis

Overview of this book

Odoo is fast becoming the reference open source platform for business applications thanks to the fact that it provides the infrastructure needed for developers to deliver software solutions for any business process quickly. Odoo's layered module approach makes it particularly effective for combining and extending features. This updated fifth edition is a tutorial-style introduction to essential Odoo development topics. The book starts by covering the development essentials for building business applications and takes you through Odoo installation and configuration, gradually transitioning from having no specific knowledge of Odoo to being ready for application development. You'll then learn how to develop your first Odoo application, while covering topics such as models and views. Later chapters will get you up to speed with using server APIs to add business logic, helping you lay a solid foundation for advanced topics. As you progress, you’ll get equipped to build and customize your applications and explore the new features available in Odoo 12 and beyond, such as in-memory ORM and computed writable fields. Finally, you’ll gain insights into building business logic and using the Odoo API to integrate with various applications. By the end of this book, you’ll be able to build business apps from scratch using the latest version of Odoo.
Table of Contents (22 chapters)
1
Section 1: Introduction to Odoo Development
6
Section 2: Models
9
Section 3: Business Logic
13
Section 4: Views
18
Section 5: Deployment and Maintenance

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:

  1. Fill in the basic definition values for it – enter To-do Item in the Model Description field and x_todo_item for the Model field.
  2. 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.
  3. 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.

    Figure 1.12 – The Create Fields form

    Figure 1.12 – The Create Fields form

  4. 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 the Is 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.

  5. 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:
Figure 1.13 – The database structure for the To-do Item model

Figure 1.13 – The database structure for the To-do Item model

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.