在视图中访问 many2one 字段的子值

Accessing child value of many2one field in view

为了能够 select 一个 batch/lot 更容易地根据最小生命周期要求,我想要变量 use_date (模块 product.expiry) 显示在视图中的批号旁边 'stock.view_pack_operation_lot_form'.

视图(属于模型stock.pack.operation)定义如下(默认):

<record id="view_pack_operation_lot_form" model="ir.ui.view">
<!-- ... -->
    <field name="pack_lot_ids" nolabel="1" attrs="{'readonly': [('state', '=', 'done')]}">
        <tree editable="bottom" decoration-success="qty_todo==qty"
              decoration-danger="(qty_todo &gt; 0) and (qty&gt;qty_todo)">
            <field name="lot_name" invisible="not context.get('only_create', False)"/>
            <field name="lot_id" invisible="context.get('only_create', False)"
                   domain="[('product_id','=', parent.product_id)]"
                   context="{'default_product_id': parent.product_id, 'active_pack_operation': parent.id}"/>
            <field name="qty_todo"
                   invisible="not context.get('show_reserved') or context.get('serial') or context.get('state_done')"
                   readonly="1"/>
            <field name="qty" invisible="context.get('serial')"/>
            <button name="do_minus" type="object" icon="fa-minus-square" attrs="{'invisible': [('qty', '&lt;=', 0.99)]}"
                    invisible="not context.get('show_reserved') or context.get('state_done')"/>
            <button name="do_plus" type="object" icon="fa-plus-square" attrs="{'invisible': [('plus_visible', '=', False)]}"
                    invisible="not context.get('show_reserved') or context.get('state_done')"/>
            <field name="plus_visible" invisible="1"/>
        </tree>
    </field>
<!-- ... -->
</record>

字段pack_lot_ids定义为One2many,参考模型'stock.pack.operation'中的'stock.pack.operation.lot'。 'stock.pack.operation.lot' 有一个字段 lot_id 定义为 Many2one 并引用了 'stock.production.lot'。 'stock.production.lot' 包含我要添加到视图的字段 use_date

我的第一次尝试是按如下方式添加点符号中的字段 "reference.field_name":

<record id="stock_pack_operation_lots_form_inherit" model="ir.ui.view">
   <field name="name">stock.pack.operation.lots.form.inherit</field>
   <field name="model">stock.pack.operation</field>
   <field name="inherit_id" ref="stock.view_pack_operation_lot_form"/>
   <field name="arch" type="xml">
       <field name="lot_id" position="after">
          <field name="lot_id.use_date"/>
       </field>
   </field>
</record>

导致以下错误:

Error context:
View `stock.pack.operation.lots.form`
[View_id: 722, xml_id: stock.view_pack_operation_lot_form, model: stock.pack.operation, parent_id: n / a]

然后我在 SO 上发现 and this post 建议将定义定义为子视图:

<!-- ... -->
<field name="arch" type="xml">
    <field name="lot_id" position="after">
        <field name="lot_id" nolabel="1">
            <tree>
                <field name="use_date"/>
            </tree>
        </field>
    </field>
</field>
<!-- ... -->

这次它没有抛出错误,而是添加了字段 lot_id[ 而不是字段 use_date =37=]第二次。

如有任何关于如何在批号旁边添加 use_date 字段的提示,我们将不胜感激!

您可以在模型中创建相关字段 stock.pack.operation.lot:

use_date = fields.Char(string='Use date', related='lot_id.use_date')

然后您可以将其添加到您的视图中:

<!-- ... -->
    <field name="pack_lot_ids" nolabel="1" attrs="{'readonly': [('state', '=', 'done')]}">
        <tree editable="bottom" decoration-success="qty_todo==qty"
              decoration-danger="(qty_todo &gt; 0) and (qty&gt;qty_todo)">
            <field name="lot_name" invisible="not context.get('only_create', False)"/>
            <field name="lot_id" invisible="context.get('only_create', False)"
                   domain="[('product_id','=', parent.product_id)]"
                   context="{'default_product_id': parent.product_id, 'active_pack_operation': parent.id}"/>
            <field name="use_date" />
            <field name="qty_todo"
                   invisible="not context.get('show_reserved') or context.get('serial') or context.get('state_done')"
                   readonly="1"/>
            <field name="qty" invisible="context.get('serial')"/>
            <button name="do_minus" type="object" icon="fa-minus-square" attrs="{'invisible': [('qty', '&lt;=', 0.99)]}"
                    invisible="not context.get('show_reserved') or context.get('state_done')"/>
            <button name="do_plus" type="object" icon="fa-plus-square" attrs="{'invisible': [('plus_visible', '=', False)]}"
                    invisible="not context.get('show_reserved') or context.get('state_done')"/>
            <field name="plus_visible" invisible="1"/>
        </tree>
    </field>
<!-- ... -->

希望对你有所帮助