Skip to Content
Menu
You need to be registered to interact with the community.
This question has been flagged
4 Odgovori
286 Prikazi

Please here is my function:


def _get_project_id_by_name (self, cr, uid, project_name, context=None):

obj = self.pool.get('callim_project')

obj_ids = obj.search(cr, uid, [('project', '=', project_name)])

res = obj.read(cr, uid, obj_ids, ['id'], context)

print("--------------------get_project_id_by_name-------------------------")

print (res[0]['id'])

return res[0]['id']

I got this error: List of index out of range i didnt understand its source. Please help

Avatar
Opusti

Thanks a lot for the answer. I have corrected it but it doesnt work: def _get_project_id_by_name (self, cr, uid, project_name, context=None): obj = self.pool.get('callim_project') obj_ids = obj.search(cr, uid, [('project', '=', project_name)]) res = obj.read(cr, uid, obj_ids, ['id'], context) print("--------------------get_project_id_by_name-------------------------") if res == {}: return True else: return res[0]['id']

could you please print res and post what you get,...

Best Answer

Hi,

Index out of range error occurs when you are providing an index for which a list element does not exist.

Here you have to check whether res is empty or not before printing it.(You can use if statement for this)


Hope this helps.....


Avatar
Opusti
Avtor Best Answer

I tried to do this:


def _get_project_id_by_name (self, cr, uid, project_name, context=None):

obj = self.pool.get('callim_project')

obj_ids = obj.search(cr, uid, [('project', '=', project_name)])

res = obj.read(cr, uid, obj_ids, ['id'], context)

print("--------------------get_project_id_by_name-------------------------")

print res

if res == []:

return False

else:

return res[0]['id']


And it works now, i do not have any problem.

But when sir please i need extremely to know how to do this in odoo: 

services.create(cr,uid,value,context)??

Please help

Avatar
Opusti