Enter an error trying to load Content_type natural-key snap-ins in Django

I use the --natural option when dropping one of my models into lights, so when deploying, I will not run into the Content_typ ID problem. The results are 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 the device back, 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 

It seems that the method does not accept the line to load. What am I missing?

+4
source share
1 answer

I can only guess right now, but by looking at the source code of Django and your error message, I think that the format of your device may be broken. An example that you posted is that the entire content of a file? If so, then I think you need to put this model in a list, for example (note the external brackets):

 [ { "pk": 1, "model": "seo.opportunitymetadatamodel", "fields": { "_content_type": [ "opportunity", "jobopportunity" ], "og_description": "", "description": "", "title": "test", "keywords": "", "og_title": "", "heading": "" } } ] 

Why? After Django has successfully parsed the JSON data, that data is passed to the python deserializer. This is repeated according to 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 that object_list is a json object (equivalent to a python dictionary), iterating over it will only give you the keys, in this case pk, model, field . On line 84, Django does _get_model(d["model"]) , that is, using the line "model" as an index for another line, possibly pk (which is the first element in object_list ). This is a type error.

When object_list is an actual list, iterating over it will give you dictionaries that can be indexed line by line.

+3
source

Source: https://habr.com/ru/post/1338883/


All Articles