当编辑值为空的字段时,Odoo会发出警告

2024-03-29 10:03:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在创建新产品或编辑产品时向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

当我创建产品时,代码运行正常。在

但当我编辑产品并删除所选供应商时,它不再起作用。在

有人能给我指出错误吗?谢谢!在


编辑:使用约束修复。在


Tags: inselfids编辑产品resthis供应商
2条回答

您可以添加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

创建产品时调用create函数,编辑时始终调用{}函数。在

在create中,您应该检查vals参数,如果它不符合要求,您应该警告用户更正它,然后编辑实际记录。在

试试这个

# For example boolean
if vals["myBoolean"] == False:
    raise Warning('myBoolean should be true always!')

相关问题 更多 >