更新树视图字段时如何更新视图字段的值?
我想在更新“location_qty”的值时更改“total_location_qty”的值。
这是我的对象
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
这是我的观点
<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>
答案 0 :(得分:0)
我解决了如下
参考:account / account_invoice.py(会计 - &gt;客户发票或客户退款或供应商退款)
class Demo(models.TransientModel):
_name = 'demo.parent'
@api.one
@api.depends('manual_pick_lines_ids.qty_pick_base_uom')
def _compute_total_amount(self):
self.total_amount = sum(line.line_total_amount for line in self.demo_lines_ids)
demo_lines_ids = fields.One2many('demo.lines', 'demo_id', 'demo Items')
total_amount = fields.Float(string='Total Amount', store=True, compute='_compute_total_amount')
class DemoLines(models.TransientModel):
_name = "demo.lines"
@api.one
@api.depends('price', 'qty')
def _compute_amount(self):
self.line_total_amount = self.qty * self.price
demo_id = fields.Many2one('demo.parent', 'Demo')
location_id = fields.Many2one('stock.location', 'Stock Location', readonly=True)
qty = fields.Float(string='Qty')
price = fields.Float(string='Price')
line_total_amount = fields.Float(string='Location Qty', compute='_compute_amount', store=True)