在 odoo 中引发异常时将日期字段设置为 null



我已经验证了start_dateend_date。我需要在引发错误时设置空结束日期字段。这是我的代码:

@api.onchange('end_date')
def onchange_end_date(self, end_date, start_date):
    if (start_date and end_date) and (start_date < end_date):
        raise exceptions.except_orm(_('Warning!'),_('The start date must be less than to the end date.'))

在 Odoo 更改方法中,您无需传递任何参数,系统将直接从 self 对象获取它。

@api.onchange('end_date')
def onchange_end_date(self):
    if (self.start_date and self.end_date) and (self.start_date < self.end_date):
        raise exceptions.except_orm(_('Warning!'),_('The start date must be less than to the end date.'))

这可能会对您有所帮助。

你好,你可以试试这个,希望对你有帮助

import ValidationError
    @api.onchange('end_date')
    def onchange_end_date(self):
        if (self.start_date and self.end_date) and (self.start_date < self.end_date):
            raise ValidationError(''The start date must be less than to the end date.'))

谢谢

除了 onchange,我们可以使用约束...它有助于在创建和编辑以及更改时验证某些内容。这是我的代码,它工作正常

@api.multi
@api.constrains('start_date', 'start_date')
def _check_date(self):
    start_date = self.start_date
    end_date = self.end_date
    if (start_date and end_date) and (start_date > end_date):
        raise ValidationError(_('The start date must be less than to the end date. '))

谢谢

最新更新