我想在创建新产品或编辑产品时向suppliers表添加一个条目。每个产品都必须有一个供应商。 如果没有选择供应商,系统必须给出警告:“您应该填写供应商详细信息,至少一个。”
这是我的代码:
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约束,该约束将在修改给定字段时执行。在
有关约束的详细信息:click here
创建产品时调用}函数。在
create
函数,编辑时始终调用{在create中,您应该检查
vals
参数,如果它不符合要求,您应该警告用户更正它,然后编辑实际记录。在试试这个
相关问题 更多 >
编程相关推荐