[Answer]-Problems with Django and Parsing a json

1👍

Your dict is not coming out fine. Look at it:

{u"{u'name': u'Pooled Set 1', u'tags': [u\'thing1\', u\'thing2\'], u'sets': …}

You have a dict whose keys are JSON strings representing dicts (and whose values you haven’t shown us). 'name' isn’t a key in that dict; "{u'name': u'Pooled Set 1', u'tags': [u\'thing1\', u\'thing2\'], u'sets': …}" is.

Without knowing what the original data are, or even what the rest of the parsed myDict looks like (you’ve only shown us part of the first key in the dict), it’s hard to be sure what exactly you should be writing instead of what you’ve actually written.

You could of course do something like this:

for key, value in myDict.items():
    actual_dict = json.loads(key) # does the value mean anything?
    print actual_dict['name']

… but parsing data incorrectly and then trying to fix them up after the fact is almost always a bad idea.

0👍

Have you considered using fixtures?
Given that you already have a json file in the proper format of the django model,
you can just run

python manage.py loaddata fixture_name.json

and it will directly update your model

Reference:
https://docs.djangoproject.com/en/dev/howto/initial-data/

Leave a comment