user2742861
user2742861

Reputation: 340

Retrieve ID python

I'm trying to retrieve the current ID from a postgresql database using openERP. This is my function (inside a class):

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

Then, I am calling the function this way:

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

But I'm getting following error:

TypeError: get_id() takes exactly 2 arguments (7 given)

what am I doing wrong?

Upvotes: 0

Views: 138

Answers (2)

Heroic
Heroic

Reputation: 980

You can do it like this.

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

Upvotes: 1

user1576199
user1576199

Reputation: 3207

You have not defined function syntax correctly.

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

Do some thing like this

please read this doc. https://doc.openerp.com/v6.0/developer/2_5_Objects_Fields_Methods/field_type.html

Hope this will be helpful.

Upvotes: 1

Related Questions