Skip to content Skip to sidebar Skip to footer

How Should I Connect Dictionary's User_id & Model's User_id?

I wanna parse excel& make dictionary and connect the model(User) which has same user_id of dictionary. Now dictionary is dict_data = {'user_id': 1,'nationarity': America, 'dorm

Solution 1:

for data in dict_datas:        
    user = User.object.filter(user_id = data['user_id']).exists()
    if user:
       user.__dict__.update(**dict_data)
       user.save() 

Solution 2:

dict_data you posted is a dict,you shouldn't iterate it like a list. I guess your dict_data is a list of dict, so:

fordatain dict_datas:
    user = User.objects.get(user_id=data['user_id'])
    user.name_id = data['**']
    ...
    user.save()

First, fetch the user object with user_id in your xecel&dict, then change the value, and save it.

Post a Comment for "How Should I Connect Dictionary's User_id & Model's User_id?"