gpt4 book ai didi

python - Odoo - 在编辑具有空值的某些字段时发出警告

转载 作者:太空宇宙 更新时间:2023-11-04 02:49:07 24 4
gpt4 key购买 nike

如果创建新产品或编辑产品,我想在供应商表中添加一个条目。每个产品都必须有一个供应商。如果未选择供应商,系统必须给出警告“您应该至少填写一个供应商详细信息。”

这是我的代码:

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

关于python - Odoo - 在编辑具有空值的某些字段时发出警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44362902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com