I have made a one2many field to add the spare parts of a vehicle in a form and i want to modify the product qty in the stock based on the ordered qty so the available qty = the available qty - the ordered qty the constraints match the first condition but not the else case
class One2manyProductLine(models.Model): _inherit = 'fleet.vehicle.log.services'
custom_field = fields.One2many("product.line.details", "new_field", string="Test", copy=True, auto_join=True)
class ProductLineDetails(models.Model): _name = 'product.line.details'
product_id = fields.Many2one('product.product', string="Product")
avail_qty = fields.Float(related='product_id.qty_available', string="Quantity")
exact_cost = fields.Float(related='product_id.standard_price')
product_desc = fields.Text()
ordered_qty = fields.Float(store=True)
total = fields.Float(compute='_compute_total')
new_field = fields.Many2one('fleet.vehicle.log.services')
@api.constrains('ordered_qty')
def _check_ordered_qty(self):
for rec in self:
if rec.ordered_qty > rec.avail_qty:
raise ValidationError("There is no enough amount in stock")
else:
self.avail_qty = rec.avail_qty - rec.ordered_qty