在sale.order.line模型
中,您可以通过 2 种方式进行操作:
- 使用 @api.constrains 装饰器。
- 重写写入/创建函数。
这是每种方式的代码示例:
- 使用 @api.constrains :
from openerp.exceptions import ValidationError
@api.constrains('product_id')
def constr(self):
a=0
for rec in self.order_id.order_line:
if (rec.product_id.id == self.product_id.id) and (rec.id != self.id):
a=a+1
if a > 1:
raise ValidationError(u"Duplicate lines \nthis line already exist!\ncheck your lines again please!")
- 使用覆盖写入/创建功能:
@api.multi
def write(self,vals):
if 'product_id' in vals :
prod = self.env['product.product'].browse([vals.get('product_id')])
else:
prod = self.product_id
for line in self.env['sale.order'].browse([self.order_id.id]).order_line:
if (prod.id == line.product_id.id) and line.id != self.id:
raise ValidationError(u"Duplicate lines \nthis line already exist!\ncheck your lines again please!")
return super(sale_order_line,self).write(vals)
@api.model
def create(self,vals):
for line in self.env['sale.order'].browse([vals['order_id']]).order_line:
if (vals.get('product_id') == line.product_id.id) and line.id != self.id:
raise ValidationError(u"Duplicate lines \nthis line already exist!\ncheck your lines again please!")
return super(sale_order_line,self).create(vals)
谢谢你的问题
最好的问候。