Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
4 Trả lời
282 Lượt xem

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

Ảnh đại diện
Huỷ bỏ

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

Câu trả lời hay nhất

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


Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ