Skip to Content
Menu
This question has been flagged
1 Reply
85274 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!

Avatar
Discard
Best Answer

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.

Avatar
Discard
Author

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