Pages

Sunday, November 4, 2012

Error "string indices must be integers" When Deserializing Queryset


Situation:
Attempting to deserialize a django 1.5 queryset

from django.core import serializers
queryset = MyModel.objects.all()
data = serializers.serialize('json', queryset)
#... on another server, data loaded using urllib2:
obj = serializers.deserialize('json', data) # error

Problem:
Django returns the error "string indices must be integers"

Solution:
You are not deserializing what you think you are deserializing.  Look at the JSON string and it should be apparent.   I had this problem in two cases:

  1. I had accidentally serialized twice (the JSON string contained escaped quotation marks: \")
  2. There was an error message instead of the actual object


References:

  1. Django Docs:  Serialization

No comments:

Post a Comment