1

我想创建一个在用户输入无效日期时引发错误的函数,所以如果我们输入当前日期之前的日期,它将显示错误,有什么想法吗?

我的课看起来像:

class business_trip(orm.Model):
_columns = {'start_trip': fields.datetime('Trip Starts on:',translate=True, required=False),
        'start_business': fields.datetime('Business Starts on:',translate=True, required=False),
        'end_business': fields.datetime('Business Ends on:',translate=True, required=False),
        'end_trip': fields.datetime('Trip Ends on:',translate=True, required=False),
4

2 回答 2

1

您可以添加添加 Python 约束,例如,

@api.one
@api.constrains('start_trip', 'start_business', 'end_business', 'end_trip')
def _check_date(self):
    now = datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)
    if self.start_trip and  now < self.start_trip:
        raise exceptions.ValidationError("Entered Date Should be greter then Today")
    elif self.start_business and now < self.start_business:
        raise exceptions.ValidationError("Entered Date Should be greter then Today")
    elif self.end_business and now < self.end_business:
        raise exceptions.ValidationError("Entered Date Should be greter then Today")
    elif self.end_trip and now < self.end_trip:
        raise exceptions.ValidationError("Entered Date Should be greter then Today")
于 2015-05-14T11:11:56.273 回答
1

谢谢各位,这是我的解决方案:

  def _check_date(self,cr,uid,ids,context=None):

        record=self.browse(cr,uid,ids,context=None)
        for data in record:
            if data.start_trip >= data.start_business or data.start_trip >= data.end_business or data.start_trip >= data.end_trip or data.start_business >= data.end_business or data.start_business >= data.end_trip or data.end_business >= data.end_trip:
                return False
            else:
                return True

_constraints = [(_check_date, 'Error: You Entered Wrong Dates, Please Enter the Right Dates ',['start_trip', 'start_business', 'end_business', 'end_trip'])]
于 2015-05-21T08:50:28.563 回答