Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
4 Răspunsuri
287 Vizualizări

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

Imagine profil
Abandonează

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,...

Cel mai bun răspuns

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.....


Imagine profil
Abandonează
Autor Cel mai bun răspuns

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

Imagine profil
Abandonează