开发者

Serializing objects containing django querysets

Django provides tools to serialize querysets (django.core.serializers), but what about serializing querysets living inside other objects (like dictionaries)?

I want to serialize the following dictionary:

dictionary = { 'alfa': queryset开发者_如何学C1, 'beta': queryset2, } 

I decided to do this using simplejson (comes with django). I extended simplejson.JSONEncoder the following way:

from django.utils import simplejson
from django.core import serializers

class HandleQuerySets(simplejson.JSONEncoder):
     """ simplejson.JSONEncoder extension: handle querysets """
     def default(self, obj):
         if isinstance(obj, QuerySet):
             return serializers.serialize("json", obj, ensure_ascii=False)

         return simplejson.JSONEncoder.default(self, obj)

Then I do: simplejson.dumps( dictionary, cls=HandleQuerySets), but the returned dicionary looks like this:

{ "alfa": "[{\"pk\": 1, \"model\": \"someapp.somemodel\", \"fields\": {\"name\": \"alfa\"}}]",
  "beta": "[{\"pk\": 1, \"model\": \"someapp.somemodel\", \"fields\": {\"name\": \"alfa\"}}]" }

Django-generated JSON is inserted to the dictionary as string, not JSON. What am I doing wrong?


The correct way to do this would be:

from django.utils import simplejson
from django.core import serializers
from django.db.models.query import QuerySet

class HandleQuerySets(simplejson.JSONEncoder):
     """ simplejson.JSONEncoder extension: handle querysets """
     def default(self, obj):
         if isinstance(obj, QuerySet):
             return serializers.serialize("python", obj, ensure_ascii=False)
         return simplejson.JSONEncoder.default(self, obj)

Because serializers.serialize("json", [...]) returns a string ; if you ask for the python serializer, you get a dictionnary, and json encodes whatever is returned by your encoder's default method. Look at the json documentation for details.

You will have to handle more types in your encoder class (such as datetime objects), but you get the idea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜