请在 Odoo 中纠正我的以下代码
Please correct me on the following code in Odoo
这是 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'])
]
请大家指正。
谢谢
使用新的 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",
),
]
这是 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'])
]
请大家指正。 谢谢
使用新的 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",
),
]