python - Odoo:动态设置未存储的字段

标签 python odoo

我有一个字段“qty_av”,我根据字段“branch_product_ids”值通过 onchange 方法动态设置,只要我尚未保存记录,“qty_av”就会成功设置 但是一旦记录被保存,“qty_av”就会变成null

之前:

enter image description here

之后:

enter image description here

我的代码

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/

相关文章:

python - Heroku Python 本地环境变量

python - (代码)OpenERP v7中product.product中的id是什么?

odoo - 将消息添加到 message_post odoo 9

javascript - Odoo - 在特定 View 上自动刷新页面

python - 如何找到字符串中字符的ascii值?

python - 将列组织和排序到字典变量中

python - 根据另一个文件中保存的列表选择文件中的变量

python - 如何在python中模拟依赖

google-sheets - 如何将 Google 电子表格连接到 Odoo?

javascript - Odoo javascript onclick 事件