Odoo 10 - 扩展视图以包含相关模型字段

Odoo 10 - Extend view to include related model field

我正在尝试扩展在 Odoo 10 中显示包的视图,以便它也显示 product_id:

<record id="stock_view_picking_form_enhanced" model="ir.ui.view">
    <field name="name">stock_view_picking_form_enhanced</field>
    <field name="model">stock.picking</field>
    <field name="inherit_id" ref="stock.view_picking_form"/>
    <field name="priority">20</field>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='pack_operation_product_ids']/tree/field[@name='package_id']" position="after">
            <field name="package_id.product_id"/>
        </xpath>
    </field>
</record>

所以基本上我希望显示 pack_operation_product_id 中的相关字段 product_id

实施此方法的正确方法是什么?

首先,您必须在继承的 python class.

中包含相关字段
class StockPicking(models.Model):
    _inherit = "stock.picking"

    product_id = fields.Many2one('product.product', related='package_id.product_id', string='Product', store=True)

然后在XML中可以这样写:

<record id="stock_view_picking_form_enhanced" model="ir.ui.view">
    <field name="name">stock_view_picking_form_enhanced</field>
    <field name="model">stock.picking</field>
    <field name="inherit_id" ref="stock.view_picking_form"/>
    <field name="priority">20</field>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='pack_operation_product_ids']/tree/field[@name='package_id']" position="after">
            <field name="product_id"/>
        </xpath>
    </field>
</record>