yogakumboro yogakumboro - 3 months ago 25
Python Question

odoo get the value of maximum array index

I got a function to append data into array :

def _get_state(self, cr, uid, context=None):
idemployee = _default_employee(self, cr, uid, context=None)
sql = " SELECT C.id AS id, C.sequence, C.name \
FROM wf_group_member A \
LEFT JOIN wf_group B ON B.id = A.group_id \
LEFT JOIN wf_process BB ON BB.id = B.process_id\
LEFT JOIN wf_state C ON C.group_id = B.id \
LEFT JOIN hr_employee D ON D.id = A.member_id \
WHERE LOWER(code) = 'ca' AND member_id = %s ORDER BY sequence "
res = []
cr.execute(sql, [(idemployee)])
ardata = cr.fetchall()
for data in ardata:
res.append((data[1], data[2]))
return res


and then I tried to get the value of maximum array index :

def _get_maxstate(self, cr, uid, context=None):
res = []
arr_state = _get_state(self, cr, uid, context)
states = len(arr_state) - 1
res = arr_state[0][states]
return res


But when I call _get_maxstate in action button, it raise error :

res = arr_state[0][states]
IndexError: tuple index out of range


What's wrong with my code, help me please

Answer

states is an index to the last element of arr_state. However you do not index arr_state with states, you index it with 0 and then index the result with states. It seems like you probably want to just index the other way around, i.e. arr_state[states][0].