如果创建新产品或编辑产品,我想在供应商表中添加一个条目。每个产品都必须有一个供应商。 如果未选择供应商,系统必须给出警告“您应该至少填写一个供应商详细信息。”
这是我的代码:
class warning_supplier(models.Model):
_inherit = 'product.template'
@api.multi
def write(self, vals):
res = super(warning_supplier, self).write(vals)
supplier_id = self.env['res.partner'].search([('name','=','No Supplier')])
for this in self:
seller_ids = this.seller_ids
if len(seller_ids)==0:
raise Warning('You should fill in the supplier details, at least one.')
return res
当我创建产品时,代码运行正常。
但是当我编辑产品并删除所选供应商时,它不再起作用了。
有人可以向我指出错误吗?谢谢!
编辑:通过使用约束修复。
请您参考如下方法:
您可以添加 python 约束,该约束将在修改给定字段时执行。
class product_template(models.Model):
_inherit = 'product.template'
@api.multi
@api.constrains('seller_ids')
def onchange_seller(self):
for record in self :
if not record.seller_ids :
raise ValidationError("You should fill in the supplier details, at least one.")
return
有关约束的更多信息:click here
