我有一个父模型和一个子模型,这是代码:
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}
在child_model中,我成功地将事件onchange get_subtotal中的“金额”和“数量”相乘,这是xml:
<tree >
<field name="item" />
<field name="amount" on_change="get_subtotal(amount, qty)" />
<field name="qty" on_change="get_subtotal(amount, qty)" />
<field name="subtotal" />
</tree>
我的问题是如何使用 onchange 事件计算 child_model 中的小计(总和)并将值存储到parent_model 中的 grand_total 字段中。 需要帮助,谢谢
最佳答案
第一:将所有内容移至新的 api。
将 onchange 添加到 Hook 到 line_ids
的父模型,例如:
@api.onchange('line_ids')
def _onchange_line_ids(自身):
关于python - 如何使用 odoo 9 中的 onchange 事件对 TreeView 行中的值求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39700777/