我有一个字段“qty_av”,我根据字段“branch_product_ids”值通过 onchange 方法动态设置,只要我尚未保存记录,“qty_av”就会成功设置 但是一旦记录被保存,“qty_av”就会变成null
之前:
之后:
我的代码
branch_product_ids = fields.Many2one(comodel_name="custom.product", string="Product",
domain="[('branch_line.branch_id.user_lines.user_id','=', user_id)]")
user_id = fields.Many2one('res.users', 'Current User', default=lambda self: self.env.user.id)
branch_id = fields.Many2one('custom.branch', string="Branch", required=False, compute="_get_branch", store=True)
qty_av = fields.Integer('Av Qty', readonly=True, store=True)
@api.onchange('branch_product_ids')
def onchange_product(self):
selected_lines = []
if self.branch_product_ids:
for rec in self:
selected_lines = rec.env['custom.branch.line'].search(
['&', ('product_id', '=', rec.branch_product_ids.id), ('branch_id', '=', rec.branch_id.id)]).mapped(
'qty')
if not selected_lines:
raise ValidationError(
_('Current branch has ZERO stock on this product, Please select an available Product'))
else:
rec.qty_av = selected_lines[0]
最佳答案
因为您将其设为只读,这就是原因。
要存储只读字段,请尝试此操作。
Python 中:
qty_av = fields.Integer('Av Qty')
在 XML 中:
<field name="qty_av" readonly="1" force_save="1"/>
关于python - Odoo:动态设置未存储的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59820736/