How to sum values in treeview line using onchange event in odoo 9
<p>I got a parent model and a child model, here is the code :</p>

<pre><code>class parent_model(osv.osv):
    _name = 'parent_model'
    _columns = {
        'line_ids'      : fields.one2many('child_model', 'line_id', 'Line ID', ondelete='cascade'),
        'description'   : fields.text('Description', required=True),
        'grand_total'   : fields.float('Grand Total'),
    }

class child_model(osv.osv):
    _name = 'child_model'
    _columns = {
        'line_id'       : fields.many2one('parent_model', string='Line ID', required=True),
        'item'          : fields.char('Item', required=True),
        'amount'        : fields.float('Amount', required=True),
        'qty'           : fields.integer('Qty', required=True),
        'subtotal'      : fields.float('Total', readonly=True),
    }

    def get_subtotal(self, cr, uid, ids, num1, num2, context=None):
        res = {}
        if num1 and num2:
            res['subtotal'] = num1 * num2
        return {'value': res}
</code></pre>

<p>In child_model I successfully multiplying "amount" and "qty" in event onchange get_subtotal, here is the xml :</p>

<pre><code>&lt;tree &gt;
    &lt;field name="item" /&gt;
    &lt;field name="amount" on_change="get_subtotal(amount, qty)" /&gt;
    &lt;field name="qty" on_change="get_subtotal(amount, qty)" /&gt;
    &lt;field name="subtotal" /&gt;
&lt;/tree&gt;
</code></pre>

<p>My question is how to calculate(sum) subtotal in child_model and store the value into grand_total field in parent_model using onchange event. 
Need help please, Thanks</p>

<p>1st: move everything to new api.</p>

<p>Add an onchange <strong>to the parent model</strong> hooked to <code>line_ids</code>, like:
<code>
@api.onchange('line_ids')
def _onchange_line_ids(self):
</code></p>

python openerp odoo-9