In Odoo, one of the prominent functionalities is the capability to develop custom views, enabling users to engage with data in unique and inventive manners. In this blog post, we will discuss how to add different view modes for an existing model in Odoo.
Every model will have a different view mode with a predefined view in it, like form view, tree view, etc. While inheriting that model views, the default views with given fields will be added. Therefore, we can utilise this to have a distinct display mode for that model with the required fields from that model. For example, while inheriting the ir.model.fields’s form and tree views it will contain many fields that may not be necessary. So to make it the desired view, we can create a different view mode for that module.
So to create a different view mode for an existing model, let’s create the records for the desired view to show a tree view with field name, model name, and a form view for it with an one2many field selection_ids which shows all selections of a selection field.
<!--Tree view for ir.model.fields-->
<record id="ir_model_fields_view_tree" model="ir.ui.view">
<field name="name">ir.model.fields.view.tree</field>
<field name="model">ir.model.fields</field>
<field name="arch" type="XML">
<tree string="Selection Fields Tree View">
<field name="name"/>
<field name="model_id"/>
</tree>
</field>
</record>
<!--Form view for ir.model.fields with tree view inside for one2many of ir.model.fields.selection-->
<record id="ir_model_fields_view_form" model="ir.ui.view">
<field name="name">ir.model.fields.view.form</field>
<field name="model">ir.model.fields</field>
<field name="arch" type="XML">
<form string="Selection Fields Form View">
<field name="selection_ids">
<tree string="Selections One2Many Tree View"
editable="bottom">
<field name="sequence" widget="handle"/>
<field name="value"/>
<field name="name"/>
</tree>
</field>
</form>
</field>
</record>
Here we created a form view and a tree view for ir.model.fields. Now inherit the model with eval function for field view_ids:
<record id="selection_field_action" model="ir.actions.act_window">
<field name="name">Selection Fields</field>
<field name="res_model">ir.model.fields</field>
<field name="view_mode">tree,form</field>
<field name="search_view_id" ref="base.view_model_fields_search"/>
<field name="domain">[('ttype','=','selection')]</field>
<field name="view_ids"
eval="[(5, 0, 0),(0, 0, {'view_mode': 'tree',
'view_id': ref('ir_model_fields_view_tree')}),
(0, 0, {'view_mode': 'form',
'view_id': ref('ir_model_fields_view_form')})]"/>
</record>
Here while inheriting cleared the default views of the ir.model.fields using one2many operation 5,0,0, which is to unlink all records from one2many field view_ids and created new records to it with the reference to the record id we have created before. If only one view_mode is required instead of field view_ids we can use view_id with reference to the record id for the view.
For example, if only the tree view is required:
<field name="view_id" ref="ir_model_fields_view_tree"/>
So, the final views of examples are:
Form View:
Tree View:
By following these steps and incorporating the provided code lines into your Odoo implementation, you will be able to create a new view for a model while inheriting.