Reputation: 6517
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 ?
Upvotes: 5
Views: 1796
Reputation: 12214
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):
[
{
"pk": 1,
"model": "seo.opportunitymetadatamodel",
"fields": {
"_content_type": [
"opportunity",
"jobopportunity"
],
"og_description": "",
"description": "",
"title": "test",
"keywords": "",
"og_title": "",
"heading": ""
}
}
]
Why? After Django parsed the JSON data successfully, this data is passed to the python deserializer. This iterates over the data as follows:
82 for d in object_list:
83 # Look up the model and starting build a dict of data for it.
84 Model = _get_model(d["model"])
http://code.djangoproject.com/browser/django/trunk/django/core/serializers/python.py#L82
Now imagine object_list
is a json object (equivalent to python's dictionary), iterating over it will only get you the keys, in this case pk, model, field
. In line 84 Django does _get_model(d["model"])
, that is, using a string "model"
as index to another string, probably pk
(which is the first element in object_list
). That's a type error.
When object_list
is an actual list, iterating over it will give you dictionaries, which can be indexed by strings.
Upvotes: 4