Skip to Content
Menu
This question has been flagged
2 Replies
9505 Views

How to update the value of field of from view when update the field of tree view?

I want to change the value of "total_location_qty" when I update the value of "location_qty".

This is my obj

class Demo(models.TransientModel):
    _name = 'demo.parent'
    demo_lines_ids = fields.One2many('demo.lines', 'demo_id', 'demo Items')
    total_location_qty = fields.Float(string='Total of location qty')

class DemoLines(models.TransientModel):
    _name = "demo.lines"
demo_id = fields.Many2one('demo.parent', 'Demo')
location_id = fields.Many2one('stock.location', 'Stock Location', readonly=True)
location_qty = fields.Float(string='Location Qty')

@api.onchange('location_qty')
def on_change_location_qty(self):
self.demo_id.total_location_qty = self.location_qty


This is my view

<record id="view_demo_wizard" model="ir.ui.view">
<field name="name">Demo</field>
<field name="model">demo.parent</field>
<field name="arch" type="xml">
<form string="Demo Parent">
<group string="Demo">
<group colspan="4">
<field name="total_location_qty" readonly="1"/>
</group>
<field name="demo_lines_ids" nolabel="1" >
<tree string="Demo Lines" editable="bottom" create="false" delete="false" >
<field name="location_qty"/>
</tree>
</field>
</group>
</form>
</field>
</record>
Avatar
Discard

Hi did you try the answer?

Best Answer

Hi,

Try like this:

class Demo(models.TransientModel):
_name = 'demo.parent'
demo_lines_ids = fields.One2many('demo.lines', 'demo_id', 'demo Items')
total_location_qty = fields.Float(compute='_get_total_qty', string='Total of location qty')

@api.one
@api.depends('demo_lines_ids.location_qty')
def _get_total_qty(self):
s=0
for rec in self.demo_lines_ids:
s = s + rec.location_qty
self.total_location_qty = s


class DemoLines(models.TransientModel):
_name = "demo.lines"
demo_id = fields.Many2one('demo.parent', 'Demo')
location_id = fields.Many2one('stock.location', 'Stock Location', readonly=True)
location_qty = fields.Float(string='Location Qty')
Avatar
Discard
Best Answer

You define method  @api.onchange('demo_lines_ids') in the model demo.parent instead of @api.onchange('location_qty')

Avatar
Discard
Related Posts Replies Views Activity
10
May 20
7458
1
Jan 16
5291
1
May 21
3665
2
Nov 18
13303
5
Jan 17
3301