Django: Serializing models in a nested data structure?

Posted by Rosarch on Stack Overflow See other posts from Stack Overflow or by Rosarch
Published on 2010-05-03T21:54:28Z Indexed on 2010/05/03 21:58 UTC
Read the original article Hit count: 261

It's easy to serialize models in an iterable:

def _toJSON(models):
    return serializers.serialize("json", models, ensure_ascii=False)

What about when I have something more complicated:

[
(Model_A_1, [Model_B_1, Model_B_2, Model_B_3]),
(Model_A_2, [Model_B_3, Model_B_4, Model_B_5, Model_B_59]),
(Model_A_3, [Model_B_6, Model_B_7]),
]

I tried serializing each model as it was added to the structure, then serializing the whole thing with simplejson.dumps, but that causes the JSON defining each model to be escaped.

Is there a better way to do this?

© Stack Overflow or respective owner

Related posts about django

Related posts about database