如何创建一个函数以在 openerp 中引发错误
Posted
技术标签:
【中文标题】如何创建一个函数以在 openerp 中引发错误【英文标题】:how to create a function to raise error in openerp 【发布时间】:2015-07-24 11:57:34 【问题描述】:我想创建一个在用户输入无效日期时引发错误的函数,所以如果我们输入的日期早于当前日期,它会显示错误,有什么想法吗?
我的班级看起来像:
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),
【问题讨论】:
【参考方案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")
【讨论】:
【参考方案2】:谢谢你们,这是我的解决方案:
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'])]
【讨论】:
以上是关于如何创建一个函数以在 openerp 中引发错误的主要内容,如果未能解决你的问题,请参考以下文章