如何创建一个函数来引发 openerp 中的错误
how to create a function to raise error in openerp
我想创建一个函数,当用户输入无效日期时引发错误,所以如果我们输入当前日期之前的日期,它会显示错误,有什么想法吗?
我的 class 看起来像:
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),
您可以添加 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")
谢谢大家,这就是我的解决方案:
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'])]
我想创建一个函数,当用户输入无效日期时引发错误,所以如果我们输入当前日期之前的日期,它会显示错误,有什么想法吗?
我的 class 看起来像:
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),
您可以添加 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")
谢谢大家,这就是我的解决方案:
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'])]