Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Odoo 11 Development Essentials

You're reading from   Odoo 11 Development Essentials Develop and customize business applications with Odoo 11

Arrow left icon
Product type Paperback
Published in Mar 2018
Publisher Packt
ISBN-13 9781788477796
Length 336 pages
Edition 3rd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Daniel Reis Daniel Reis
Author Profile Icon Daniel Reis
Daniel Reis
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Quick Start – The Odoo Developer Mode and Concepts FREE CHAPTER 2. Installing and Organizing the Development Environment 3. Your First Odoo Application – A Practical Overview 4. Models – Structuring the Application Data 5. Import, Export, and Module Data 6. The ORM API – Handling Application Data 7. Business Logic – Supporting Business Processes 8. External API – Integrating with Other Systems 9. Backend Views – Design the User Interface 10. Kanban Views and Client-Side QWeb 11. Reports and Server-Side QWeb 12. Creating Website Frontend Features 13. Debugging and Automated Tests 14. Deploying and Maintaining Production Instances 15. Other Books You May Enjoy

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.

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.

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 4,  Models – 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.

You have been reading a chapter from
Odoo 11 Development Essentials - Third Edition
Published in: Mar 2018
Publisher: Packt
ISBN-13: 9781788477796
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime