如何在odoo中正确继承继承的视图?

时间:2018-05-26 14:54:02

标签: odoo-10

我一直试图在视图中定位一个继承的元素,但它对我不起作用。

所以我有view继承自hr.contract的表单视图,如下所示:

  <record id="hr_contract_view_form_cayor" model="ir.ui.view">
    <field name="name">name</field>
    <field name="model">hr.contract</field>
    <field name="inherit_id" ref="hr_contract.hr_contract_view_form"/>
    <field name="arch" type="xml">
        <data>
            <!-- more elements define with xpath -->
            <xpath expr="//field[@name='job_id']" position="after">
                <field name="retirement_age"/>
            </xpath>
            ...
            <xpath expr="//page[@name='information']" position="after">
                <page string="Allowances" groups="hr_payroll.group_hr_payroll_user">

                    <group>
                        <group name="allowances_group1">
                            ...
                            <field name="car_allowance"/>
                            ...
                        </group>
                        <group name="allowances_group2">
                            ...
                        </group>
                    </group>
                </page>
            </xpath>
        </data>
    </field>
</record>

我已经定义了一个要从此form view继承的新文件,它会向其中添加一个新的field,如下所示:

<record id="hr_contract.hr_contract_view_form_inherited" model="ir.ui.view">
    <field name="name">hr.contract.grade.rank.form.inherit</field>
    <field name="model">hr.contract</field>
    <field name="inherit_id" ref="module.hr_contract_view_form_cayor"/>
    <field name="arch" type="xml">
         <data>
            <xpath expr="//group[@name='allowances_group1']" position="inside">
                <field name="medical_allowance" />
            </xpath> 
         </data>
    </field>
</record>

但是当我升级我的模块时,我收到以下错误:

Field `retirement_age` does not exist

如何正确地从第一个继承,并添加我的新字段?

任何帮助将不胜感激,提前感谢。

1 个答案:

答案 0 :(得分:1)

请尝试以下代码:

Python:

class HrContract(models.Model):
    _inherit = 'hr.contract'

    retirement_age = fields.Char(string="Retirement Age:")

XML:

<record id="hr_contract_view_form_cayor" model="ir.ui.view">
    <field name="name">name</field>
    <field name="model">hr.contract</field>
    <field name="inherit_id" ref="hr_contract.hr_contract_view_form"/>
    <field name="arch" type="xml">
        <data>
            <!-- more elements define with xpath -->
            <xpath expr="//field[@name='job_id']" position="after">
                <field name="retirement_age"/>
            </xpath>
        </data>
    </field>
</record>