您好,我在one2many
字段中添加了车辆备件的表格,我想根据订购数量修改库存中的产品数量,以便
可用数量=可用数量-订购数量
约束与第一个条件匹配,但与其他情况不匹配。
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