3
votes

I have a model. For example let say it is this:

class my_model(models.Model):
    _name = 'my.model'

    field1 = fields.Char('name')

It has tree and form views. Both work properly.

Now I have created new model, copying old one:

class my_model_template(models.Model):
    _name = 'my.model.template'
    _inherit = 'my.model'

Now till this part, everything is fine. It copies everything from old model. But when it comes to views..

So I did this for my 'my.model.template' views (tree and form):

<record id="view_my_model_template_tree" model="ir.ui.view">
    <field name="name">my.model.template.tree</field>
    <field name="model">my.model.template</field>
    <field name="inherit_id" ref="my_model.view_my_model_tree"/>
    <field name="arch" type="xml">
        <tree string="My Model" position="attributes">
            <attribute name="string">My Model Template</attribute>
        </tree>
    </field>
</record>

<record id="view_my_model_template_form" model="ir.ui.view">
    <field name="name">my.model.template.form</field>
    <field name="model">my.model.template</field>
    <field name="inherit_id" ref="my_model.view_my_model_form"/>
    <field name="arch" type="xml">
        <form string="My Model" position="attributes">
            <attribute name="string">My Model Template</attribute>
        </form>
    </field>
</record>

But it does not copy views properly. For example tree view only shows name field, when in original view it has four fields. In form view, it seems to show all fields, but those fields are in some random position, without any formatting (that was in old view).

1
How to copy data from one model to another ? stackoverflow.com/questions/54703551/…vbt

1 Answers

2
votes

You need to specify which view to open for which view mode. Because when you copy another model's view, it seems it does not find correct view automatically (even if only one is defined for each mode)

<record model="ir.actions.act_window.view" id="action_my_model_template_tree">
    <field name="sequence" eval="1"/>
    <field name="view_mode">tree</field>
    <field name="view_id" ref="view_my_model_template_tree"/>
    <field name="act_window_id" ref="action_my_model_template"/>
</record>     

<record model="ir.actions.act_window.view" id="action_my_model_template_form">
    <field name="sequence" eval="1"/>
    <field name="view_mode">form</field>
    <field name="view_id" ref="view_my_model_template_form"/>
    <field name="act_window_id" ref="action_my_model_template"/>
</record> 

Note Also if you will use such model anywhere in other models views and you try to open it's form directly from that other view, it will also open "unformatted" view. To bypass this too, you need to specify view to open:

For example like this:

<record id="view_my_other_model_form" model="ir.ui.view">
    <field name="name">my.other.model.form</field>
    <field name="model">my.other.model.</field>
    <field name="arch" type="xml">
        <form string="My Other Model">
            <field name="my_model_template_id" 
                context="{'form_view_ref': 'my_model_template.view_my_model_template_form'}"/>
        </form>
    </field>
</record>