Form views
Next, we will create the form view:
- Create another View record, using the following values:
-
- View Name: To-do Form View
- View Type: Form
- Model: x_todo_item
- View Name: To-do Form View
- In the Architecture tab, type the following XML code:
<form>
<group>
<field name="x_name" />
<field name="x_is_done" />
<field name="x_work_team_ids"
widget="many2many_tags"
context="{'default_x_is_work_team': 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 10, Backend Views – Design the User Interface. Here, we also chose a specific widget for the work team field, to be displayed as tag buttons instead of a list grid.
We added the widget attribute to the Work Team field, to have the team members presented as button-like tags.
By default, relational fields allow you to directly create a new record to be used in the relationship. This means that we are allowed to create new Partner directly from the Work Team field. But if we do so, they won't have the Is Work Team? flag enabled, which can cause inconsistencies.
For a better user experience, we can have this flag set by default for these cases. This is done with the context attribute, used to pass session information to the next View, such as default values to be used. This 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.
So in our case, the expression needed to set a default value for the partner's Is Work Team? flag is {'default_x_is_work_team': True}.
That's it. If we now try the To-Do menu option, and create a new item or open an existing one from the list, we will see the form we just added.