简体   繁体   中英

KeyError: 'user_id'

I wanna put dictionary data to the model(User) .I wrote like

 for row_number, row_data in dict_data.items():
            user1 = User.objects.filter(corporation_id=row_data['corporation_id'])
            if user1:       
               user1.update(user_id=dict_data['user_id'])

dict_data is dictionary type like

{'00010': OrderedDict([('corporation_id', '00001aB3D4'), ('user_id', '11111'), ('name', 'Tom'), ('age', '20')])}

I wanna put dict_data data's user_id to user_id column of User model. Definitely, dict_data has 'user_id' , but I really cannot understand why this error happens.How can I fix this?

Instead of:

dict_data["user_id"]

You need to use the unpacked element:

row_data["user_id"]

Or, in case you really want to use dict_data you have to first access the corresponding dictionary value:

dict_data[row_number]["user_id"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM