使用以下函数,我试图确定一列是否已经具有 X 值。
def error_verify(self, cr, uid, ids, context=None):
for record in self.browse(cr, uid, ids, context):
nr = int(record.nr_service) # Gets the current value from field
cr.execute("SELECT nr_service FROM services WHERE nr_service = %s", [nr])
values = cr.fetchall()
if values: # if there's ANY result it means there's at least one
# row with that value in the table
return True
return False
该函数是这样调用的:
_constraints = [
(error_verify, 'Error: Already that ID in the table', [''])
]
问题是:我没有得到任何结果。我正在尝试获取错误消息,但我无法到达那里。我将一些现有值放在“record.nr_service”中,但它不会抛出任何消息。我的问题是:我做对了吗?
我也尝试过这种方式:
def error_verify(self, cr, uid, ids, context=None):
for record in self.browse(cr, uid, ids, context):
nr = int(record.nr_service) # Gets the current value from field
cr.execute("SELECT nr_service FROM services WHERE nr_service = %s", [nr])
values = cr.fetchall()
for n in values:
if nr == int(n[0]):
return True
return False
在这种情况下,也不会抛出任何消息。
我该如何解决?