这是Odoo中的以下代码,并不像我预期的那样工作。
def _check_dates(self, cr, uid, ids, context=None):
for rec in self.browse(cr, uid, ids, context=context):
if rec.rdate and rec.gdate :
start = date.strftime(str(rec.rdate), "%Y-%m-%d")
end = date.strftime(str(rec.gdate), "%Y-%m-%d")
if start >= end:
return False
return True
_constraints = [
(_check_dates, 'Error ! Received Date must be less or equal than given Date', ['rdate'])
]
请纠正我的任何人。 感谢
答案 0 :(得分:1)
使用新的Odoo 8 ORM API(无需向_constraints
列表中添加任何内容(现已弃用):
@api.one
@api.constrains('rdate', 'gdate')
def _check_dates(self):
if self.rdate and self.gdate:
start = fields.Date.from_string(self.rdate)
end = fields.Date.from_string(self.gdate)
if start > end:
raise exceptions.ValidationError("Error! Received Date must be less or equal than given Date")
注意:我将start >= end
更改为start > end
,使其与您的错误消息一致(因为之前的代码不接受相同的日期)。
或者,您可以将其设置为SQL约束,这样可以在更深层的数据库级别上进行操作:
_sql_constraints = [
(
'rdate_gdate_check',
'check(rdate <= gdate)',
"Error! Received Date must be less or equal than given Date",
),
]