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.