PAWAN SHARMA PAWAN SHARMA - 2 months ago 31
Python Question

'Int' value is not return in function for odoo 8

I want to show total numbers of record on screen. For this i have write query language for function and i get desire result on Ubuntu 14.04 command line screen by printing that value from python code. But when i return this value in function then it shows me error pop-up message "TypeError: 'int' object is not iterable". So, anyone can give me proper solution for this problem. My python code for function and field is below.

Python code:

def get_count(self, cr, uid, ids, sequence, arg, context=None):
cr.execute('SELECT count(*) from sun_helpdesk')
u_data = cr.fetchall()
print "cr:", u_data
print "res:",int(u_data[0][0])
return int(u_data[0][0])

_columns = {
'sequence': fields.function(get_count, method=True, string='Total Tickets', type='integer'),
}


XML code:
In XML this field added simply.


Result on the Ubuntu Screen is:

cr: [(101L,)]

res: 101

new_values = dict(values)

TypeError: 'int' object is not iterable

Answer Source

You need to return a dict which its keys are ids, for example:

def get_count(self, cr, uid, ids, sequence, arg, context=None):

    res = {}

    cr.execute('SELECT count(*) from sun_helpdesk')
    u_data = cr.fetchall()
    print "cr:", u_data
    print "res:",int(u_data[0][0])

    #res[id] must be set to False and not to None because of XML:RPC
    # "cannot marshal None unless allow_none is enabled"
    res[ids[0]] = int(u_data[0][0]) if ids else False
    return res

Take a look at Function field