0

I have a dictionary in python and I would like to insert values from it into a database to populate my Db quickly. Every time I try this I get the following error: ValueError: too many values to unpack (expected 2)

Here's what I have:

def post(self, request):
    image = "no image"
    characters = {
        "Auron": {'name': "Auron",
                   'stars': 4,
                   'image': image,
                   'series': "FF10"},
        "Barret": {'name': "Barret",
                   'stars': 3,
                   'image': image,
                   'series': "FF7"},
        "Zidane":  {'name': "Zidane",
                   'stars': 5,
                   'image': image,
                   'series': "FF9"}
    }
    for key, value in characters:
        number = 3
        name = value.name
        stars = value.stars
        image = value.image
        series = value.series
        Characters.objects.create(id=number,name=name,stars=stars,image=image,series=series)
        number += 1

    return JsonResponse("Added all chars", safe=False)

Also, this code runs after an ajax call. Why is this happening? I have googled this but couldn't find anything similar to my problem. What could be causing this?

Thanks

davidb
  • 1,503
  • 4
  • 30
  • 49
  • 1
    https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops – jbch Dec 21 '17 at 23:16
  • Duplicate of: https://stackoverflow.com/questions/5466618/too-many-values-to-unpack-iterating-over-a-dict-key-string-value-list – jbch Dec 21 '17 at 23:19
  • Possible duplicate of [Iterating over dictionaries using 'for' loops](https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops) – mrhallak Dec 21 '17 at 23:27

1 Answers1

0
for key, value in characters.iteritems():

Reason is that you only get the keys if you iterate over a dict. You need to explicitly iterate over items() or iteritems() (the later using less memory in general)

>>> d=dict(a=1, b=2)
>>> d
{'a': 1, 'b': 2}
>>> [x for x in d]
['a', 'b']
>>> [x for x in d.iteritems()]
[('a', 1), ('b', 2)]
>>> [x for x in d.items()]
[('a', 1), ('b', 2)]

Note how iteritems and items are tuples and the straight iteration over a dictionary is not?

You can learn more at https://www.python-course.eu/dictionaries.php

Or straight from the source https://docs.python.org/2/library/stdtypes.html#dict

MultiSkill
  • 406
  • 4
  • 8
  • Hi, I still don't fully understand this. When I call this I get for example Key="Auron", value ="'name': "Auron",'stars': 4,'image': image,'series': "FF10"". How can I get only the values of keys "name", "stars" etc? – davidb Dec 21 '17 at 23:38
  • for value in characters.itervalues(): see https://docs.python.org/2/library/stdtypes.html#dict.itervalues – MultiSkill Dec 21 '17 at 23:41
  • Sorry, I still don't get it. I can't get the values from the dictionaries inside the main dictionary. I checked your links and the links to other threads and still nothing. – davidb Dec 21 '17 at 23:57