Hi, I have a model which contains a parent and several children.
The parent has a computed field "product_remaining", which is updated from the modification of a different field in any of the children ("lot_qty").
The field "product_remaining" is shown in a tree for all children, using a "related" field in the children's model.
Everything is working fine, except that the tree only updates the "product_remaining" field in the row for which the modification has been done, and it does not update any other line.
Furthermore, if I click to modify a line which has not been updated by a previous change, and leave without modify it, the line gets updated correctly.
Is any way to update/refresh all the affected lines in the tree view, and not only the one where the change has been made?
I am using Odoo v10.
Here is the relevant code:
Python:
class Te2PackLotOperation(models.TransientModel):
_name = 'te2.pack.lot.operation'
parent_id = fields.Many2one('te2.pack.product.operation', readonly=True, ondelete='cascade')
product_id = fields.Many2one('product.product', 'Product', related='parent_id.product_id')
product_qty = fields.Float('Total', readonly=True, related='parent_id.product_qty')
product_remaining = fields.Float('Remaining', readonly=True, related='parent_id.product_remaining')
lot_id = fields.Many2one('stock.production.lot')
lot_qty = fields.Float('Done')
packing = fields.Char('Packing')
@api.onchange('lot_qty')
def onchange_lot_qty(self):
rec = self._origin
rec.write({'lot_qty': self.lot_qty})
class Te2PackProductOperation(models.TransientModel):
_name = 'te2.pack.product.operation'
product_id = fields.Many2one('product.product', 'Product', readonly=True)
product_qty = fields.Float('Total', readonly=True)
product_remaining = fields.Float('Remaining', readonly=True, compute='_product_remaining_get')
lot_ids = fields.One2many('te2.pack.lot.operation', 'parent_id')
@api.one
@api.depends('lot_ids', 'lot_ids.lot_qty')
def _product_remaining_get(self):
already_done = 0
for lot in self.lot_ids:
already_done += lot.lot_qty
self.product_remaining = self.product_qty - already_done
XML:
<record id="te2_stock_picking_advanced_view_form" model="ir.ui.view">
<field name="name">te2.pack.operation.form</field>
<field name="model">te2.pack.product.operation</field>
<field name="arch" type="xml">
<form string="Advanced Stock Picking">
<field name="lot_ids">
<tree editable="bottom" create="false" delete="false"
decoration-danger="product_remaining!=0" decoration-success="product_remaining==0"
default_order="product_id">
<field name="product_id"/>
<field name="product_qty"/>
<field name="product_remaining"/>
<field name="lot_id"
domain="[('product_id','=', product_id)]"
/>
<field name="use_date"/>
<field name="lot_qty"/>
<field name="packing"/>
<button name="add_lot" string="Lot Split" type="object" icon="fa-list"/>
</tree>
</field>
<footer>
<button name="save" string="Save" type="object" class="oe_highlight" />
<button string="Cancel" class="oe_link" special="cancel"/>
</footer>
</form>
</field>
</record>