Book Image

Odoo 11 Development Essentials - Third Edition

By : Daniel Reis
Book Image

Odoo 11 Development Essentials - Third Edition

By: Daniel Reis

Overview of this book

Odoo continues to gain worldwide momentum as the best platform for open source ERP installations. Now, with Odoo 11, you have access to an improved GUI, performance optimization, integrated in-app purchase features, and a fast-growing community to help transform and modernize your business. With this practical guide, you will cover all the new features that Odoo 11 has to offer to build and customize business applications, focusing on the publicly available community edition. We begin with setting up a development environment, and as you make your way through the chapters, you will learn to build feature-rich business applications. With the aim of jump-starting your Odoo proficiency level, from no specific knowledge to application development readiness, you will develop your first Odoo application. We then move on to topics such as models and views, and understand how to use server APIs to add business logic, helping to lay a solid foundation for advanced topics. The book concludes with Odoo interactions and how to use the Odoo API from other programs, all of which will enable you to efficiently integrate applications with other external systems.
Table of Contents (20 chapters)
Title Page
Packt Upsell
Contributors
Preface
Index

Adding Menus, Models, and Views


Now we will be creating new application features, rather than extending already existing ones. We will continue working on our Library project.

First, we want to add a Library top menu, and we can already add the Books Authors menu item to it. Next, we will create a new Model for Books, and make it available to users by adding a menu item for it. Finally, we will create the list and form views for the Books model.

Creating menus

We now have a way to list Authors. It is the list of all Partners with the Is Book Author? flag checked.

Now, we want to add an Authors menu item that opens that list, filtering away the other Partners that are not Authors. The good news is that we can do this pretty easily by reusing the already existing Partner views.

Before that, we want to create the top menu item for our shiny new Library app, under which we will be including the menu item for the Authors, and later a menu item for Books.

The menu definitions can be seen in the Settings app, at Technical | User Interface | Menu Items. Create a new menu item using the following values:

  • Menu: Library
  • Parent Menu: (empty)
  • Action: (empty)

In the Submenus tab, click on Add an item to add a sub-menu using these values:

  • Menu: Book Authors
  • Parent Menu: Library (the default value)
  • Action: Select ir.actions.act_window, and in the selection list on the right click on Create and Edit, opening a form to create the related Window Action. Set the following values on the Window Action:
    • Action name: Book Authors
    • Object: res.partner (the technical name of the target Model):

Save all the forms we opened, and the menu tree for our Library app should be ready to use:

To see changes in the menu, we need to reload the web client. We can see that the menu is a tree of menu items, with parent/child relations. The leaf menu items have a related Action, defining what happens when it is selected. This Action name is what will be used as the title of the presented views.

There are several Actions types available, and the most important ones are Window, Reports, and Server Actions. Window Actions are the most frequent ones, and are used to present Views in the web client. Report Actions are used to run reports and Server Actions are used to define automated tasks.

At this point, we are concerned with Window Actions that are used to display views.

The Menu Item we just created for Book Authors uses a Window Action, which was created directly from the Menu Item form. We can also view and edit it from the Settings | Technical | Actions menu options. In this particular case, we are interested in the Window Actions menu option.

It happens that the Book Authors Action we created is too simple, and does not perform what we want: it opens a list with all Partners, regardless of whether they have the Is Book Author? set. We need to fix that.

Open the Window Actions menu item, look up our recently created Book Authors action, and edit it. We are interested in the Filters section, found inside the General Settings tab.

Note

In many cases, it is more convenient to use the Edit Action option in the Debug menu, providing a convenient shortcut to edit the Window Action that was used to access the current view.

The Domain Value field can have an expression defining a filter for the records to be presented. This "domain expression" follows an Odoo-specific syntax that will be explained in later chapters. For now, it's enough to know that it is a list of triplets, where each triplet is a filter condition.

In our case, the expression to use for the Domain Value is:

[('x_is_book_author', '=', True)]

For usability, we would also want new records created to have the Is Book Author? flag checked by default. An Action is able to do this, allowing us to set default values on the views it presents. This is done using the Context Value. The Context is the way Odoo passes around session information, including default values to be used. It will be discussed in detail in later chapters, and for now we just need to know that it is a dictionary of key-value pairs. Values prefixed with default_ provide the default value for the corresponding field.

In our case, the expression needed for the Context Value is:

{'default_x_is_book_author': True}

That's it. If we now try the Library | Book Authors menu option, it should list only Partners with the Is Book Author? flag checked, if any. And if we try to create a new Book Author, we will see that the Is Book Author? checkbox will be conveniently checked by default.

Note

The Domain filter we used can't be removed by the user. It is possible to set default filters that can be removed, using the Context to enable a default Search filter instead. This is done with a search_default_<field> key, as explained in Chapter 9, Backend Views – Design the User Interface.

Creating a Custom Model

We now have a Library top menu with a menu item for Book Authors. It's time to add a Books menu item to hold the records for books.

Let's visit again, in Settings, the Technical | Database Structure | Models menu, and click on Create. Fill in the Model form with these values:

  • Model Description: Book
  • Model: x_library_book

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. For us, it makes sense to use it for the book title. You may edit it and change the Field Label to a more meaningful Title.

We also add a field for the book authors. A book can have many authors, and any author can have many books. So this is a many-to-many relationship.

On the Fields list, click on Add an item to create a new field with these values:

  • Field Name: x_author_ids
  • Field Label: Authors
  • Field Type: many2many
  • Object Relation: res.partner
  • Domain: [('x_is_book_author', '=', True)]

The many-to-many field has a few specific definitions: the Relation Table, Column1, and Columns2 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 4Models – Structuring the Application Data.

The Domain attribute is optional, but we used it so that only book authors are selectable from the list. Otherwise, all Partners would be available for selection.

We now need to make this Model available in the user interface. If we create a menu item for a new model, it can be used right away, since Odoo will automatically generate default views for it.

In Settings, navigate to Technical | User Interface | Menu Items and create a new record with the following values:

  • Menu: Books
  • Parent Menu: Library
  • Action: Select ir.actions.act_window, and in the selection list on the right, click on Create and Edit to open a form for the creation of the related Window Action:
    • Action name: Books (will be the title used for the presented views)
    • Object: x_library_book (the technical name of the target Model)

Save the Action and Menu Item, and once we reload the web client the Library top menu app will show the Books option alongside the Book Authors option.

Try it and you will see the basic, but functional, automatically generated views. We will want to create our own views, so that's what we will be working on in the next section.

Creating Views

We have created the Books model and made it available in the user interface with a Menu Item. Next, we will be creating the two essential views for it: a list (also called a tree) and a form.

In Settings, navigate to Technical | User Interface | Views and create a new record with the following values:

  • View Name: Books List View
  • View Type: Tree
  • Model: x_library_book

In the Architecture tab, we should write XML with the view structure. Use the following XML code:

<tree>
  <field name="x_name" />
  <field name="x_author_ids" widget="many2many_tags" />
</tree>

The basic structure of a list view is quite simple: a <tree> element containing one or more <field> elements for each of the columns to display in the list view. We added the widget attribute to the Authors field, to have it presented as button-like tags.

We can do a few more interesting things with list views, and will explore them in more detail in Chapter 9, Backend Views – Design the User Interface.

Next, we will create the form view. Create another new record, and use the following values:

  • View Name: Books Form View
  • View Type: Form
  • Model: x_library_book

In the Architecture tab, type the following XML code:

<form>
  <group>
    <field name="x_name" />
    <field name="x_author_ids" 
           widget="many2many_tags" 
           context="{'default_x_is_book_author': True}" />
  </group>
</form>

The form view structure has a root <form> element, containing elements such as <field>, among others that we will learn about in Chapter 9, Backend Views – Design the User Interface. Here, we also chose a specific widget for the Authors field, to be displayed as tag buttons instead of a list grid. We also added a context attribute, so that when new Authors are created directly from here, they will already have the Is Book Author checkbox enabled.