Enable Dark Mode!
how-to-add-different-view-modes-for-an-existing-model-in-odoo-16.jpg
By: Mohamed Aslam AK

How to Add Different View Modes for an Existing Model in Odoo 16

Technical Odoo 16

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:

how-to-add-different-view-modes-for-an-existing-model-in-odoo-16-1-cybrosys

Tree View:

how-to-add-different-view-modes-for-an-existing-model-in-odoo-16-2-cybrosys

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.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message