Why serializer only works for QuerySet

50 views
Skip to first unread message

Leon Sasson

unread,
Nov 3, 2013, 1:09:49 PM11/3/13
to django...@googlegroups.com
Django serializer works really well when you need to serialize a QuerySet, but often times I need to serialize a JSON object with more than just a QuerySet.
This leads me to serialize the QuerySet, and then load it again to a python dict and then serialize it again. Like this:

peopleJSON = serializers.serialize("json", people_queryset)
people_dict = simplejson.loads(peopleJSON )
json = simplejson.dumps({'foo': foo, 'people':people_dict})

A better way would be:
data = {'foo':foo, 'people':people_queryset}
json = serializers.serialize("json", data)

Is there a reason not to allow this behavior? I can't think of one.

Daniel Roseman

unread,
Nov 3, 2013, 3:11:14 PM11/3/13
to django...@googlegroups.com
You can pretty much get what you want already by using the `python` serializer and then dumping the whole load to JSON:

    qs = serializers.serialize("python", people_queryset)
    json = simplejson.dumps({"foo": foo, "people": qs})

--
DR. 

Leon Sasson

unread,
Dec 19, 2013, 2:35:51 PM12/19/13
to django...@googlegroups.com
That would definitely work.
I guess it's missing from the docs — only 'xml', 'json', and 'yaml' formats are documented. The 'python' serialization format was documented until version 1.1, but then removed, although it is still implemented as of 1.6.
Reply all
Reply to author
Forward
0 new messages