Python选择匹配OpenERP

2024-04-20 02:01:34 发布

您现在位置:Python中文网/ 问答频道 /正文

通过下面的函数,我试图确定一个列是否已经有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

函数的调用方式如下:

^{pr2}$

问题是:我没有得到任何结果。我试图得到错误信息,但我无法到达那里。我把一些现有的值放在record.nr_服务“,但它不会抛出任何消息。我的问题是:我的功能做得对吗?在

我也试过这样做:

 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

在这种情况下,也不会抛出任何信息。在

我该怎么解决呢?在


Tags: theinselfidsforuidreturnvalue
2条回答

好吧,我一直在测试,我想我找到了我的错误所在。假设数据库中有值“3”。所以,每当我在字段中写入值“3”时,它应该会显示一条错误消息,否则它会释放我。在

这是有效的:

def error_verify(self, cr, uid, ids, context=None):
        cr.execute("SELECT nr_service FROM services WHERE nr_service = %s", (3,))
        values = cr.fetchall()
        if values:
            return False # Returning false means that there's already a value "3" in the database.
        return True

这也适用于:

^{pr2}$

这里的问题是,我不能在查询中告诉值“3”,我应该从一个字段->OpenERP field->中获取该值,如下所示:

for record in self.browse(cr, uid, ids, context=context): # loop through the fields
    nr_value = record.nr_service # this is the field
    cr.execute("SELECT nr_service FROM services WHERE nr_service = %s", (nr_value,))
    values = cr.fetchall()
    if values:
      return False
return True

通过这段代码,它会检索错误消息(在第一篇文章中)不管我在字段中输入的值是多少。所以,问题一定在循环中。但我真的需要从这个字段中得到值。在

谢谢!在

解决了。在

def verify_service(self, cr, uid, ids, context=None):
        record = self.browse(cr, uid, ids)[0]
        rec_value = str(record.nr_service)
        cr.execute("SELECT COUNT(*) FROM services WHERE nr_service = %s", [rec_value])
        values = cr.fetchone()
        if values[0] <= 1:
            return True
        return False

相关问题 更多 >