I am using the –natural option when dumping one of my model into a fixtures, so that I won’t run into Content_typ ID problem when deploying. Results is here:
{
"pk": 1,
"model": "seo.opportunitymetadatamodel",
"fields": {
"_content_type": [
"opportunity",
"jobopportunity"
],
"og_description": "",
"description": "",
"title": "test",
"keywords": "",
"og_title": "",
"heading": ""
}
}
But when I try to load back the fixture, I get the following error:
Problem installing fixture 'seo/fixtures/initial_data.json': Traceback (most recent call last):
File "/Users/xx/dev/envs/xx/lib/python2.6/site-packages/django/core/management/commands/loaddata.py", line 167, in handle
for obj in objects:
File "/Users/xx/dev/envs/xx/lib/python2.6/site-packages/django/core/serializers/json.py", line 38, in Deserializer
for obj in PythonDeserializer(simplejson.load(stream), **options):
File "/Users/xx/dev/envs/xx/lib/python2.6/site-packages/django/core/serializers/python.py", line 84, in Deserializer
Model = _get_model(d["model"])
TypeError: string indices must be integers, not str
Seems like the method does not accept a string to load. What am I missing ?
I can only guess right now, but after looking at Django’s source code and your error message I think the format of your fixture might be broken. The example you posted, is that the whole content of the file? If yes, then I think you need to put that model in a list, like this (pay attention to the outer brackets):
Why? After Django parsed the JSON data successfully, this data is passed to the python deserializer. This iterates over the data as follows:
Now imagine
object_listis a json object (equivalent to python’s dictionary), iterating over it will only get you the keys, in this casepk, model, field. In line 84 Django does_get_model(d["model"]), that is, using a string"model"as index to another string, probablypk(which is the first element inobject_list). That’s a type error.When
object_listis an actual list, iterating over it will give you dictionaries, which can be indexed by strings.