跳至内容
菜单
此问题已终结
4 回复
312 查看

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

形象
丢弃

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

最佳答案

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


形象
丢弃
编写者 最佳答案

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

形象
丢弃