检索python的ID

检索python的ID,python,postgresql,openerp,Python,Postgresql,Openerp,我正在尝试使用openERP从postgresql数据库检索当前ID。这是我的函数(在类中): 然后,我以这种方式调用函数: 'last_id':fields.function(get_id, method = True, string = 'ID Number', type = 'integer') 但我得到了以下错误: TypeError:get_id()正好接受2个参数(给定7个) 我做错了什么?您没有正确定义函数语法 def get_id (self, cr, uid, ids, fi

我正在尝试使用openERP从postgresql数据库检索当前ID。这是我的函数(在类中):

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

'last_id':fields.function(get_id, method = True, string = 'ID Number', type = 'integer')
但我得到了以下错误:

TypeError:get_id()正好接受2个参数(给定7个)


我做错了什么?

您没有正确定义函数语法

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
做这样的事

请看这份文件


希望这会有帮助。

你可以这样做

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

我已经尝试了你告诉我的,尽管现在我收到了这个错误:“Keyerror:2”。我看过你给我的链接,我尝试了一些方法,但没有成功。我的代码与第一篇文章相同,但由于您对函数进行了更改。实际上,您的代码是有效的,这是我的错误。然而,这并不是我真正想要的,因为我并不完全清楚我想要什么。每当我启动表单创建新用户时,我只想显示(而不是写入)该新用户的当前ID。我想得到最后一个ID,然后求和+1。对于您的代码,每当我尝试创建新用户时,字段从0开始,只有当我单击按钮“创建”时,它才会显示ID。此外,我不想创建另一列(最后一个ID),我想使用当前列(ID)。我怎样才能做到这一点?也许只有XML?谢谢
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