简体   繁体   中英

Odoo doesn't show the field label in xpath

I'm adding a custom string field (stock_value) to the 'product.supplierinfo' module in Odoo enterprise 11 but I can't get it to display the label correctly.

I inherited the module and then added a new field to the module and to the view trough xpath.

Problem: the string related to the new field is not shown.

Module:

class class_name(models.Model):
    _inherit                        = 'product.supplierinfo'
    stock_value                     = fields.Integer(string="Stock")

View:

<!-- Stock value in the vendors -->
<record id="view_product_product_supplierinfo_form_view" model="ir.ui.view">
    <field name="name">product.supplierinfo.product.form</field>
    <field name="model">product.supplierinfo</field>
    <field name="inherit_id" ref="product.product_supplierinfo_form_view"/>
    <field name="arch" type="xml">
    <xpath expr="//field[@name='price']" position="after">
        <field name="stock_value" />
    </xpath>
    </field>
</record>

Result: as you can see theres a zero bellow the price value but the string label 'Stock' is not shown.

在此处输入图片说明

Other thing is tried:

Adding the next code:

<separator />
<label for="stock_value" string="Stock Value"/>

Gives me

在此处输入图片说明

Putting the field inside a group gives me

在此处输入图片说明

I also tried changing the position to 'before' in this last view but I cannot manage to make it look as it should. I tried using @string but that no longer works.

Thank you in advantage for your help.

The problem is that the field price is inside a div container, so you have to put your field after this div (which is the parent of the field price in the DOM). Thus, you have to tell xpath that you want to put your field after the DOM parent of the field price , not just after the field as you have in your code. Depending on the style you are looking for, you can select any of the following options:

Option 1 (you could add class="oe_inline to your field too"):

<!-- Stock value in the vendors -->
<record id="view_product_product_supplierinfo_form_view" model="ir.ui.view">
    <field name="name">product.supplierinfo.product.form</field>
    <field name="model">product.supplierinfo</field>
    <field name="inherit_id" ref="product.product_supplierinfo_form_view"/>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='price']/.." position="after">
            <label for="stock_value"/>
            <div>
                <field name="stock_value"/>
            </div>
        </xpath>
    </field>
</record>

Option 2 :

<!-- Stock value in the vendors -->
<record id="view_product_product_supplierinfo_form_view" model="ir.ui.view">
    <field name="name">product.supplierinfo.product.form</field>
    <field name="model">product.supplierinfo</field>
    <field name="inherit_id" ref="product.product_supplierinfo_form_view"/>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='price']/.." position="after">
            <field name="stock_value"/>
        </xpath>
    </field>
</record>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM