检索ID python

时间:2013-09-09 10:08:57

标签: python postgresql openerp

我正在尝试使用openERP从postgresql数据库中检索当前ID。这是我的功能(在课堂内):

def get_id(self, cr):
        cr.execute("SELECT id FROM table ORDER BY id DESC LIMIT 1")
        id = cr.fetchone()[0]
        return id

然后,我用这种方式调用函数:

'last_id':fields.function(get_id, method = True, string = 'ID Number', type = 'integer')

但是我收到了以下错误:

  

TypeError:get_id()只需要2个参数(给定7个)

我做错了什么?

2 个答案:

答案 0 :(得分:1)

您尚未正确定义函数语法。

def get_id (self, cr, uid, ids, fields, arg, context=None):
        res = {} 
        cr.execute("SELECT id FROM table ORDER BY id DESC LIMIT 1")
        id = cr.fetchone()[0]
        res[ids[0]] = id
        return res

做这样的事情

请阅读此文档。 https://doc.openerp.com/v6.0/developer/2_5_Objects_Fields_Methods/field_type.html

希望这会有所帮助。

答案 1 :(得分:1)

你可以这样做。

def get_id(self, cr, uid, ids, fields, arg, context=None):
     res = {}
     for field in fields:
         cr.execute("SELECT id FROM table ORDER BY id DESC LIMIT 1")
         id = cr.fetchone()[0]
         res[ids[0]][field] = id
    return res