How can I delete the “sheet” node keeping its cont

2019-08-12 16:02发布

问题:

I would like to remove the node <sheet></sheet> from a form view. For instance, I have this view:

<record id="view_account_period_form" model="ir.ui.view">
    <field name="name">account.period.form</field>
    <field name="model">account.period</field>
    <field name="arch" type="xml">
        <form string="Account Period">
            <header>
                [...]
            </header>
            <sheet>
                <group>
                    <group>
                        <field name="name"/>
                        <field name="fiscalyear_id" widget="selection"/>
                        <label for="date_start" string="Duration"/>
                        <div>
                            <field name="date_start" class="oe_inline" nolabel="1"/> -
                            <field name="date_stop" nolabel="1" class="oe_inline"/>
                        </div>
                    </group>
                    <group>
                        <field name="code"/>
                        <field name="special"/>
                        <field name="company_id" widget="selection" groups="base.group_multi_company"/>
                    </group>
                </group>
            </sheet>
        </form>
    </field>
</record>

I would like to convert it in this other view without the node, but keeping all the elements within it:

<record id="view_account_period_form" model="ir.ui.view">
    <field name="name">account.period.form</field>
    <field name="model">account.period</field>
    <field name="arch" type="xml">
        <form string="Account Period">
            <header>
                [...]
            </header>

            <group>
                <group>
                    <field name="name"/>
                    <field name="fiscalyear_id" widget="selection"/>
                    <label for="date_start" string="Duration"/>
                    <div>
                        <field name="date_start" class="oe_inline" nolabel="1"/> -
                        <field name="date_stop" nolabel="1" class="oe_inline"/>
                    </div>
                </group>
                <group>
                    <field name="code"/>
                    <field name="special"/>
                    <field name="company_id" widget="selection" groups="base.group_multi_company"/>
                </group>
            </group>

        </form>
    </field>
</record>

Is that possible or I need to override the complete code again?

Maybe something similar to:

<xpath expr="//form/sheet" position="replace">
    <!-- [...] -->
</xpath>

There is an open issue in Git Hub asking for solving this here, but I think that maybe anyone knows how to do it without programming a new feature in Odoo.

回答1:

Just use fields_view_get:

    from lxml import etree
    def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
        res = models.Model.fields_view_get(self, cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
        if view_type == 'form':
            doc = etree.XML(res['arch'])
            for sheet in doc.xpath("//sheet"):
                parent = sheet.getparent()
                index = parent.index(sheet)
                for child in sheet:
                    parent.insert(index, child)
                    index += 1
                parent.remove(sheet)
            res['arch'] = etree.tostring(doc)
        return res

improved in case of oe_chatting presence



标签: odoo odoo-8