Is it possible to create alternate views to existing models in Odoo?

198 Views Asked by At

I have scoured the Odoo 16 documentation and searched Google for answers to this.

All I've found are examples of altering/modifying/overriding existing "tree, form, kanban" views. Nothing I find shows creating a brand new view for a model while retaining the existing views.

The odoo documentation tutorial follows this process:

  1. Create Model
  2. Create Action and bind it to Model <--- what!?
  3. Create View for the Model

The problem with the Odoo architecture demonstrated above is that actions go to models instead of views -- which makes views and models tightly-coupled. It looks impossible to make lots of different views for a single model and link actions to those views. Is it impossible to make new views and use them from different actions?

My specific use case is that I want to expose the Contact model view a custom Tree View and Form View in a custom module, but I do NOT want to show the full Contact information with all fields. I want to show a very small subset of fields.

If anyone can show documentation or clear code that does this, I would greatly appreciate it.

2

There are 2 best solutions below

0
On BEST ANSWER

Use ir.actions.act_window.view as indicated in Window Actions section:

If you plan to allow multiple views for your model, prefer using ir.actions.act_window.view

Example:

<record model="ir.actions.act_window.view" id="test_action_tree">
   <field name="sequence" eval="1"/>
   <field name="view_mode">tree</field>
   <field name="view_id" ref="view_test_tree"/>
   <field name="act_window_id" ref="test_action"/>
</record>
1
On

If you want to expose the Contact model view a custom Tree View and Form View in a custom module, but I do NOT want to show the full Contact information with all fields. Kindly follow the steps I'm suggesting.

1. Crate View File : Create a view file in your custom module, i.e. custom_module/views/my_view_file.xml

2. Add views, action and menu in your custom module

<?xml version="1.0" encoding="utf-8"?>
<odoo>
<!-- Tree View Code -->
<record id="view_partner_tree_custom" model="ir.ui.view">
    <field name="name">res.partner.tree.custom</field>
    <field name="model">res.partner</field>
    <field eval="8" name="priority"/>
    <field name="arch" type="xml">
        <tree>
            <! List Of Fields you Want to Show in Tree View==!>
        </tree>
     </field>
</record>
<!-- Form View Code -->
<record id="view_partner_form_custom" model="ir.ui.view">
    <field name="name">res.partner.form.custom</field>
    <field name="model">res.partner</field>
    <field name="arch" type="xml">
        <form>
            <! List Of Fields you Want to Show in Form View ==!>
            <! You can design form view however you want. ==!>
        </form>
    </field>
</record>

<!-- Action Code -->
<record id="action_partner_form_custom" model="ir.actions.act_window">
    <field name="name">Custom Contacts</field>
    <field name="res_model">res.partner</field>
    <field name="view_mode">tree,form</field>
    <field name="search_view_id" ref="view_res_partner_filter"/>
</record>

<!-- If your menu does not show your form and tree view but take any other form and tree view then you can bind your form view and tree views with following act windo view records -->

<record id="action_custom_partner_form_view" model="ir.actions.act_window.view">
    <field eval="2" name="sequence"/>
    <field name="view_mode">form</field>
    <field name="view_id" ref="view_partner_form"/>
    <field name="act_window_id" ref="action_partner_form_custom"/>
</record>
<record id="action_custom_partner_tree_view" model="ir.actions.act_window.view">
    <field name="sequence" eval="1"/>
    <field name="view_mode">tree</field>
    <field name="view_id" ref="view_partner_tree"/>
    <field name="act_window_id" ref="action_partner_form_custom"/>
</record>   

<!-- Menu Code -->
<menuitem
    id="menu_custom_contact"
    name="Custom Contacts"
    action="action_partner_form_custom"
    sequence="5"
/>
    
</odoo>

3. Notes Make sure your view file is mentioned in manifest.py under data key. Make sure your logged in user has right to read res.partner model. Make sure there is no duplicate view, action or menuitem with same id and model. It might cause conflict.

Let me know if this helped.