检索 ID 蟒蛇



我正在尝试使用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')

但是我收到以下错误:

类型错误:get_id() 恰好需要 2 个参数(给定 7 个)

我做错了什么?

你可以

这样做。

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

您没有正确定义函数语法。

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

希望这会有所帮助。

最新更新