2
I have this piece of code in Django, the intention is to insert, if there is already the ds_user_id
then it updates the user with this ds_user_id
instead of creating a new
ds_user_id = int(post_login['ds_user_id'])
csrftoken = post_login['csrftoken']
cookies = json.dumps(str(post_login['cookies']))
defaults = {
'username': username,
'password': password,
'ds_user_id': ds_user_id,
'csrftoken': csrftoken,
'cookies': cookies,
'status': 0
}
try:
obj = User.objects.get(ds_user_id=ds_user_id)
for key, value in defaults.keys():
setattr(obj, key, value)
obj.save()
except User.DoesNotExist:
new_values = {
'username': username,
'password': password,
'ds_user_id': ds_user_id,
'csrftoken': csrftoken,
'cookies': cookies,
'status': 0
}
new_values.update(defaults)
obj = User(**new_values)
obj.save()
The insert
worked however, when trying to log in again, I get:
Too Many values to unpack (expected 2)
I’m not getting it very well, but I think it’s this line that’s bugling my code:
for key, value in defaults.keys():
Yes, it is exactly this line that is "bugging". When doing
defaults.keys()
you get only dictionary keys, not values. If you want both you should usedefaults.items()
.– Woss
@Andersoncarloswoss, taking advantage of the topic, I would like to improve the question not to receive negative, I’m almost being blocked :( but I have no time
– user148010
Apergutna is ok. The answer is simple and correct - Anderson’s - that you should have put as an answer. .
– jsbueno