1
votes

Odoo lets you extend models easily using the _inherit field. Common code and fields of the base model can thus be accessed in extending models.

My question is: can I structure my xml files (e.g. search views, form views, etc.) of the extending models to also reuse common xml code of the base models?

I have read that the template include mechanism (using t-call) does only work for QWeb templates, but not in general for xml views (see include templates). And the view inheritance using inherit_id as I understand, only extends an existing view for a given model. However it does not make it possible to include parts of existing views to create a new one.

So does this mean I have to copy the common xml code for fields in the base model to all views that extend this model?

Example:

Model inheritance

class Base(models.Model):
    _name = 'bla.base'
    common1 = fields.Text()
    common2 = fields.Text()

class ExtA(models.Model):
    _name = 'bla.exta'
    _inherit = ['bla.base']
    field_x = fields.Integer()

class ExtB(models.Model):
    _name = 'bla.extb'
    _inherit = ['bla.base']
    field_y = fields.Integer()

Views

<record model="ir.ui.view" id="exta_search">
    <field name="name">exta.search</field>
    <field name="model">bla.exta</field>
    <field name="arch" type="xml">
        <search>
            <field name="field_x"/>
            <!-- Also include xml to search in base model -->
        </search>
    </field>
</record>

<record model="ir.ui.view" id="extb_search">
    <field name="name">extb.search</field>
    <field name="model">bla.extb</field>
    <field name="arch" type="xml">
        <search>
            <field name="field_y"/>
            <!-- Also include xml to search in base model -->
        </search>
    </field>
</record>
1
What about _inherits (delegation) inheritance instead? View inheritance will work there. An example is Odoo's models product.template and product.product.CZoellner

1 Answers

0
votes

Yes there is a view inheritance possibility, but not with t-call which is for templates like printable documents or client view templates. The model ir.ui.view which is used for model views has a field inherit_id. Use that to extend origin views or already extended views. There a lot of examples in Odoo default apps.

Example: The module/app base has the model res.partner and form view with external id view_partner_form. Assumed you have extended the model with a new field function2 which should be showed right after the function field, you have to extend the view:

<record id="view_partner_form" model="ir.ui.view">
    <field name="name">extended by function2 short example</field>
    <field name="model">res.partner</field>
    <field name="inherit_id" ref="base.view_partner_form" /> <!-- here the inheritance happens -->
    <field name="arch" type="xml">
        <field name="function" position="after">
            <field name="function2" />
        </field>
        <!-- the same extension but with xpath instead -->
        <!-- <xpath expr="//field[@name='function']" position="after">
            <field name="function2" />
        </field> -->
    </field>
</record>

ref needs an external id of the view you want to extend/inherit. If the ID is from another module, prepend the module name like in the example.