Odoo Help

1

How to resolve "TypeError: list indices must be integers, not str" ?

By
Anirudh Lou
on 10/14/14, 11:25 PM 20,546 views

Hi, anyone can help me resolve this problem? Here is my code:

data['form'] = self.read(cr,uid,ids,['alphalist_category_type','a_alpha_type','fiscalyear_id'])
year1 = data['form']['fiscalyear_id']
date_code = re.split('-',contract.date_start)
year2 = int(date_code[0])
if data['form']['a_alpha_type'] == 'prev' and year2 < int(year1):

and upon running i get this error message:
"year1 = data['form']['fiscalyear_id']
TypeError: list indices must be integers, not str"

i tried to cast fiscalyear_id as int, but i get this error message:
"year1 = data['form'][int('fiscalyear_id')]
ValueError: invalid literal for int() with base 10: 'fiscalyear_id' "

Help, please!

1

Hi,

data['form'] = self.read(cr,uid,ids,['alphalist_category_type','a_alpha_type','fiscalyear_id']

Above line tells that data['form'] having list of the records which are return by the read method. So, the data is looks like below.

data['form'] is like : [{'id':1,'alphalist_category_type':'xx', . . .}, {'id':2,'alphalist_category_type':'xx', . . .}, {'id':3,'alphalist_category_type':'xx', . . .}]

So, when you have access it like : year1 = data['form']['fiscalyear_id'] 

In the above statement of course we are receiving an error as you have "TypeError: list indices must be integers, not str".

You can do two things.

1) Using loop you can access like below :

for data_form in data['form']:

    # your code in that you can get data 

    year1 = data_form['fiscalyear_id']  # If 'fiscalyear_id' is many2one field then you will get the data like (Id, name). To get the Id you have to wirte down data_form['fiscalyear_id'][0] and for its name data_form['fiscalyear_id'][1].

 

2) You can get the data like :

year1 = data['form'][0]['fiscalyear_id']  #It will give you the data of first record.

I hope you will get this answer and it is helpful to you.

Oh my, thank you very much sir. It really helps me. Thank you, thank you very much.

Anirudh Lou
on 10/15/14, 2:04 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)

Stats

Asked: 10/14/14, 11:25 PM
Seen: 20546 times
Last updated: 3/16/15, 8:10 AM