Window actions automatically determine the view to be used, but sometimes we want an action to open a specific view.
We will create a basic form view for the partner model and make the window action specifically open it.
<record id="form_all_customers" model="ir.ui.view"> <field name="name">All customers</field> <field name="model">res.partner</field> <field name="arch" type="xml"> <form> <group> <field name="name" /> </group> </form> </field> </record>
<record id="action_all_customers_form" model="ir.actions.act_window.view"> <field name="act_window_id" ref="action_all_customers" /> <field name="view_id" ref="form_all_customers" /> <field name="view_mode">form</field> <field name="sequence">10</field> </record>
Now if you open your menu and click some partner in the list, you should see the very minimal form we just defined.
This time, we used the generic XML code for any type of record that is, the record
element with the required attributes id
and model
. As earlier, the id
attribute is an arbitrary string that must be unique for your addon. The model
attribute refers to the name of the model you want to create. Given that we want to create a view, we need to create a record of model ir.ui.view
. Within this element, you set fields as defined in the model you chose via the model
attribute. For ir.ui.view
, the crucial fields are model
and arch
. The model
field contains the model you want to define a view for, while the arch
field contains the definition of the view itself. We'll come to its contents in a short while.
The name
field, while not strictly necessary, is helpful when debugging problems with views, so set it to some string that tells you what this view is intended to do. This field's content is not shown to the user, so you can fill in all the technical hints to yourself that you deem sensible. If you set nothing here, you'll get a default containing the model name and view type.
The second record we defined works in unison with act_window
we defined previously. We know already that by setting the field view_id
there, we can select which view is used for the first view mode. But given we set the field view_mode
to the default tree, form, view_id
would have to pick a tree view, but we want to set the form view, which comes second here.
If you find yourself in a situation like this, use the model ir.actions.act_window.view
, which gives you fine grained control over which views to load for which view type. The first two fields defined here are an example of the generic way to refer to other objects; you keep the element's body empty but add an attribute called ref
which contains the XML ID of the object you want to reference. So what happens here is that we refer to our action from the previous recipe in the act_window_id
field, and refer to the view we just created in the view_id
field. Then, though not strictly necessary, we add a sequence number to position this view assignment relatively to the other view assignments for the same action. This is only relevant if you assign views for different view modes by creating multiple ir.actions.act_window.view
records.