python 字段为空时出现验证错误

7cjasjjr  于 2022-10-30  发布在  Python
关注(0)|答案(2)|浏览(161)

我创建了一个验证错误,当单击提交按钮并且字段条件为空时,该错误将显示出来。但当我尝试在字段已填充时测试它时,验证错误仍然显示出来。我的代码有什么问题?

def action_approved(self):
    for rec in self:
        expense_account = self.env['account.pettycash.voucher.wizard.line'].search([('expense_account','=',False)])
        if expense_account :
            raise ValidationError('Fill the expense account!')
        else :
            rec.state = 'approved'

我希望在字段为空时显示验证错误,并在填写时批准

j9per5c4

j9per5c41#

猜猜看,你正在检查整个表。类account.pettycash.voucher.wizard.line与当前的self有什么关系?你需要在搜索条件中也包括它。

expense_account = self.env['account.pettycash.voucher.wizard.line'].search([
('expense_account','=',False),
('relational_field', '=', self.id) #Need this condition as well.
])
w1e3prcc

w1e3prcc2#

要检查是否未设置凭证行中的某个费用帐户并引发验证错误,请循环voucher_line字段并检查是否未设置expense_accountif not line.expense_account,然后引发验证错误。

范例:

def action_approved(self):
    for rec in self:
        for line in rec.voucher_line:
            if not line.expense_account:
                raise ValidationError('Fill the expense account!')
        rec.state = 'approved'

相关问题