How it works...

At a low level, the user interface is defined by records stored in special models. The first two steps create an empty XML file to define the records to be loaded, and then add them to the module's list of data files to be installed.

Data files can be placed anywhere inside the module directory, but the convention is for the user interface to be defined inside a views/ subdirectory. Usually, the name of these files is based on the name of the model. In our case, we are creating the user interface for the library.book model, so we created the views/library_book.xml file.

The next step is to define a window action to display the user interface in the main area of the web client. The action has a target model defined by res_model, and the name attribute is used to display the title to the user when the user opens the action. These are just the basic attributes. The window action supports additional attributes, giving much more control over how the views are rendered, such as what views are to be displayed, adding filters on the records that are available, or setting default values. These are discussed in detail in Chapter 10, Backend Views.

In general, data records are defined using a <record> tag, and we created a record for the ir.actions.act_window model in our example. This will create the window actions.

Similarly, menu items are stored in the ir.ui.menu model, and we can create these with the <record> tag. However, there is a shortcut tag called <menuitem> available in Odoo, so we used this in our example.

These are the menu item's main attributes:

  • name: This is the menu item text to be displayed.
  • action: This is the identifier of the action to be executed. We use the ID of the window action we created in the previous step.
  • sequence: This is used to set the order in which the menu items of the same level are presented.
  • parent: This is the identifier for the parent menu item. Our example menu item had no parent, meaning that it is to be displayed at the top of the menu.

At this point, we haven't defined any of the views in our module. However, if you upgrade your module at this stage, Odoo will automatically create them on the fly. Nevertheless, we will surely want to control how our views look, so, in the next two steps, a form and a tree view are created.

Both views are defined with a record on the ir.ui.view model. The attributes we used are as follows:

  • name: This is a title identifying the view. In the source code of Odoo, you will find the XML ID repeated here, but if you want, you can add a more human readable title as a name.
If the name field is omitted, Odoo will generate one using the model name and the type of view. This is perfectly fine for the standard view of a new model. It is recommended to have a more explicit name when you are extending a view, as this will make your life easier when you are looking for a specific view in the user interface of Odoo.
  • model: This is the internal identifier of the target model, as defined in its _name attribute.
  • arch: This is the view architecture, where its structure is actually defined. This is where different types of views differ from each other.

Form views are defined with a top <form> element, and its canvas is a two-column grid. Inside the form, <group> elements are used to vertically compose fields. Two groups result in two columns with fields, which are added using the <field> element. Fields use a default widget according to their data type, but a specific widget can be used with the help of the widget attribute.

Tree views are simpler; they are defined with a top <tree> element that contains <field> elements for the columns to be displayed.

Finally, we added a Search view to expand the search option in the box at the top-right. Inside the <search> top-level tag, we can have the <field> and <filter> elements. Field elements are additional fields that can be searched from the input given in the search view. Filter elements are predefined filter conditions that can be activated with a click. These subjects are discussed in detail in Chapter 10, Backend Views.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset