Search code examples
odooodoo-10qweb

Accessing child value of many2one field in view


In order to be able to select a batch/lot more easily according to minimum life time requirements, I would like to have the value of the variable use_date (of module product.expiry) displayed next to the batch number in the view 'stock.view_pack_operation_lot_form'.

The view (belonging to the model stock.pack.operation) is defined as follows (default):

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

The field pack_lot_ids is defined as One2many with reference to 'stock.pack.operation.lot' in the model 'stock.pack.operation'. 'stock.pack.operation.lot' has a field lot_id which is defined as Many2one with a reference to 'stock.production.lot'. 'stock.production.lot' contains the field use_date which I want to add to the view.

My first attempt was to add the field in dot notation as follows "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>

Which caused the following error:

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]

I then found this and this post on SO suggesting a definition as subview:

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

This time it didn't throw an error, but instead of the field use_date it added the field lot_id a second time.

Any hint of how to add the use_date field next to the batch number is appreciated!


Solution

  • you could create a related field in the model stock.pack.operation.lot:

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

    and then you could add it to your 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="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>
    <!-- ... -->
    

    I hope this help you