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

时间:2017-08-16 11:12:29

标签: openerp odoo-10 qweb

为了能够根据最低生命周期要求更轻松地选择批次/批次,我希望在视图中批次编号旁边显示变量use_date(模块product.expiry)的值' stock.view_pack_operation_lot_form'

视图(属于model.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; stock.pack.operation.lot&#39;在模型&stock; pack.ope.operation&#39;。 &#39; stock.pack.operation.lot&#39;有一个字段 lot_id ,定义为Many2one,引用了&stock; stock.production.lot&#39;。 &#39; stock.production.lot&#39;包含我要添加到视图中的字段 use_date

我的第一次尝试是以点符号添加字段,如下所示&#34; reference.field_name&#34;:

<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]

然后我发现thisthis在SO上发帖,建议定义为子视图:

<!-- ... -->
<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>
<!-- ... -->

这一次它没有抛出错误,但是它不是字段 use_date ,而是第二次添加字段 lot_id

欢迎任何关于如何在批号旁边添加 use_date 字段的提示!

1 个答案:

答案 0 :(得分:2)

您可以在模型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>
<!-- ... -->

我希望这可以帮到你