Ahmed Abd El Latif
Ahmed Abd El Latif

Reputation: 233

Odoo: dynamically set field which not getting stored

I have a field "qty_av" which i set dynamically via an onchange method depending on field "branch_product_ids" value, the "qty_av" is successfully set as long as i haven't saved the record yet But once the record is saved "qty_av" is turned into null

Before:

enter image description here

After:

enter image description here

My code

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]

Upvotes: 2

Views: 163

Answers (1)

Adam Strauss
Adam Strauss

Reputation: 1999

Because you are making it read-only that is why.

To store read-only field try this.

In Python:

qty_av = fields.Integer('Av Qty')

In XML:

<field name="qty_av" readonly="1" force_save="1"/>

Upvotes: 2

Related Questions