Specify the views you want to use

There are some cases where you would like to specify a view other than the default:

  • If there are several form or tree views for an object.

  • If you want to change the form or tree view used by a relational field (one2many for example).

Using the priority field

This field is available in the view definition, and is 16 by default. By default, OpenERP will display a model using the view with the highest priority (the smallest number). For example, imagine we have two views for a simple model. The model client with two fields : firstname and lastname. We will define two views, one which shows the firstname first, and the other one which shows the lastname first.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!--
    Here is the first view for the model 'client'.
    We don't specify a priority field, which means
    by default 16.
-->
<record model="ir.ui.view" id="client_form_view_1">
    <field name="name">client.form.view1</field>
    <field name="model">client</field>
    <field name="type">form</fiel>
    <field name="arch" type="xml">
        <field name="firstname"/>
        <field name="lastname"/>
    </field>
</record>

<!--
    A second view, which show fields in an other order.
    We specify a priority of 15.
-->
<record model="ir.ui.view" id="client_form_view_2">
    <field name="name">client.form.view2</field>
    <field name="model">client</field>
    <field name="priority" eval="15"/>
    <field name="type">form</fiel>
    <field name="arch" type="xml">
        <field name="lastname"/>
        <field name="firstname"/>
    </field>
</record>

Now, each time OpenERP will have to show a form view for our object client, it will have the choice between two views. It will always use the second one, because it has a higher priority ! Unless you tell it to use the first one !

Specify per-action view

To illustrate this point, we will create 2 menus which show a form view for this client object :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!--
    This action open the default view (in our case,
    the view with the highest priority, the second one)
-->
<record
    model="ir.actions.act_window"
    id="client_form_action">
    <field name="name">client.form.action</field>
    <field name="res_model">client</field>
    <field name="view_type">form</field>
    <field name="view_mode">form</field>
</record>

<!--
    This action open the view we specify.
-->
<record
    model="ir.actions.act_window"
    id="client_form_action1">
    <field name="name">client.form.action1</field>
    <field name="res_model">client</field>
    <field name="view_type">form</field>
    <field name="view_mode">form</field>
    <field name="view_id" ref="client_form_view_1"/>
</record>

<menuitem id="menu_id" name="Client main menu"/>
<menuitem
    id="menu_id_1"
    name="Here we don't specify the view"
    action="client_form_action" parent="menu_id"/>
<menuitem
    id="menu_id_1"
    name="Here we specify the view"
    action="client_form_action1" parent="menu_id"/>

As you can see on line 19, we can specify a view. That means that when we open the second menu, OpenERP will use the form view client_form_view_1, regardless of its priority.

Note

Remember to use the module name (module.view_id) in the ref attribute if you are referring to a view defined in another module.