Brian Wigginton Lab and Nerdery

Django Fixtures: JSON Formatting

Posted on January 8, 2012

Came across an issue with Django Fixtures using the JSON format. Here was my fixture:

{
"pk": 1,
"model": "core.wine",
"fields": {
"name": "Juan Gil",
"vintage": 2009
}
}

Here's the error I was getting:

Problem installing fixture '.../core/fixtures/initial_data.json': Traceback (most recent call last):
  File ".../python2.6/site-packages/django/core/management/commands/loaddata.py", line 169, in handle
    for obj in objects:
  File "...python2.6/site-packages/django/core/serializers/json.py", line 35, in Deserializer
    for obj in PythonDeserializer(simplejson.load(stream), **options):
  File "...python2.6/site-packages/django/core/serializers/python.py", line 84, in Deserializer
    Model = _get_model(d["model"])
TypeError: string indices must be integers
view raw error.txt This Gist brought to you by GitHub.

The fix was simple, wrap the object in an array.

[
{
"pk": 1,
"model": "core.wine",
"fields": {
"name": "Juan Gil",
"vintage": 2009
}
}
]

Found the fix thanks to the following StackOverflow post: http://stackoverflow.com/a/4939147

   
Google+