3
votes

Say I have this:

<record id="mrp_bom_form_action_master_products" model="ir.actions.act_window">
  <field name="name">Master Bill of Materials</field>
  <field name="res_model">mrp.bom</field>
  <field name="view_type">form</field>
  <field name="view_mode">tree,form</field>
  <field name="view_id" eval="False"/>
  <field name="views" eval="[(False, 'tree'), (ref('mrp_bom_form_view_master'), 'form')]"/>
  <field name="search_view_id" ref="mrp.view_mrp_bom_filter"/>
</record>

According to the docs:

views

a list of (view_id, view_type) pairs. The second element of each pair is the category of the view (tree, form, graph, ...) and the first is an optional database id (or False). If no id is provided, the client should fetch the default view of the specified type for the requested model (this is automatically done by fields_view_get()). The first type of the list is the default view type and will be open by default when the action is executed. Each view type should be present at most once in the list

But it doesn't work. Instead I've done this:

<record model="ir.actions.act_window.view"
        id="mrp_bom_form_view_master_form">
  <field name="sequence" eval="1"/>
  <field name="view_mode">tree</field>
  <field name="act_window_id" ref="mrp_bom_form_action_master_products"/>
</record>

<record model="ir.actions.act_window.view"
        id="mrp_bom_form_view_master_tree">
  <field name="sequence" eval="2"/>
  <field name="view_mode">form</field>
  <field name="view_id" ref="mrp_bom_form_view_master"/>
  <field name="act_window_id" ref="mrp_bom_form_action_master_products"/>
</record>

Which works, but I don't understand why the first case doesn't.

1

1 Answers

4
votes

The views field on the ir.actions.act_window model is a non-stored computed field, without an inverse function. In other words it's read-only and is not even stored in the database. That's why when you create a new action with this field, it is not saved and just gets ignored.

The documentation is a little confusing (I also had a hard time orginaly figuring this out), but not technically wrong. This field is indeed a list of (view_id, view_type) pairs, you just can't change it directly when dealing with actions stored in database. It is generated automatically, based on view_id and view_ids fields.

You can however use the field directly with actions that are not stored in the database, but returned from the python code instead. Here's an example taken from the account_payment module:

return {
    'name': _('Entry Lines'),
    'context': context,
    'view_type': 'form',
    'view_mode': 'form',
    'res_model': 'payment.order.create',
    'views': [(resource_id,'form')],
    'type': 'ir.actions.act_window',
    'target': 'new',
}