views:

214

answers:

3

I'm working on a Django-based web service and I'm trying to figure out what the best way to do my serialization will be.

The tricky requirement, though, is that I'd like to have pretty much full control over format of, and fields contained in, the response.

For example, the Django serializers (which, unfortunately, includes the wadofstuff serializer) automatically wrap the fields in { model: "app.Model", pk: 42, fields: { ... }}, which is great for creating fixtures, but isn't great for me — I'd like full control over the output.

Additionally, I'd like a serializer that is aware of Django's objects so, for example, it will do the Right Thing with a QuerySet or ManyToManyField.

Currently I'm thinking of using django-piston's emitters.py, but my experience with django-piston has only been mediocreª, so I'd like to see if there are other options.

So, are there any other options for customizable Django serializers?

ª: It's sparsely documented and tested, and I've had some problems with the serializer.

+2  A: 

Have you looked at django-piston? It should have a bunch of stuff to make this easier.

(Not sure about serialization specifically, but Django RESTy web services.)

djc
Way to read the question, djc ;) Yes, I've looked at django-piston, and it might end up using its serializer. I'm not sure if I want to use the whole thing yet, because I've had some mediocre experiences with it AND I'm sending data to Flash (to PyAMF does a much nicer job)... But, of course, that might all change :P
David Wolever
Oops, sorry about that!
djc
+1  A: 

When I need some custom serialization really fast and my case doesn't require deserialization I just write django template that can make any format I want from list/queryset/object. You can then call render_to_string with proper context and you have your data serialized.

UPDATE: some short example

Let's say you want to get json format accepted by datatables.net plugin. Since there are some special parameters required serializing queryset using simplejson or sth else is impossible here (or might be not so easy at least). We found that fastest way to provide such structure is to create simple template like this one:

{
    "sEcho": {{sEcho}},
    "iTotalRecords": {{iTotalRecords}},
    "iTotalDisplayRecords": {{iTotalDisplayRecords}},
    "aaData":[
    {% for obj in querySet %}
    [
        "{{obj.name}}",
        "{{obj.message|truncatewords:20}}",
        "<a href=\"{% url some_view obj.id %}\">{{obj.name}}</a>"
    ]
    {% if not forloop.last %}
    ,
    {% endif %}
    {% endfor %}
    ]
}

which renders to beatiful json that we were looking for. It gives you full control over the format also. Other advantages is the abillity to modify objects fields by using built-in django filters which in our case was really usefull.

I know this is not serialization as described in books but if f you want to convert some object to a custom format this solution might be the fastest one. For some reason django developers allowed to render template to any given format not only html, so why not to use it?

Example shown above is very specific but you can generate any other format. Of course writing deserializer for that could restore object from this format might be painfull but if you don't need it...

Lukasz Dziedzia
ಠ_ಠ that sounds like a recipe for pain. Wouldn't it be easier to manually convert the data to a "basic" object then use json/whatever to serialize that?
David Wolever
no it's not. You personally mentioned that some serializers output data accoriding to their specific strcture. I use this to generate some json format that was too hard to get other way. Of course if you have idea how to write serializer - do it:)
Lukasz Dziedzia
Alright, well, maybe I'm just not creative enough. Would you mind giving a small example of what you're thinking of? For example, I've got in mind something like this: `serialized = [ { "name": person.name, "friends": [ { "name": friend.name } for friend in person.friends.all() ] } for person in people ]; return simplejson.dumps(serialized);`
David Wolever
Don't get me wrong. I am not saying that my tricky solution is recommended here. I just found it useful some time ago when I faced requirements similar to that described by you. I uptadet my post with real-life example. Hope this makes my point clearer.
Lukasz Dziedzia
+1  A: 

I am currently writing a full-featured serialization framework for Django. The aim is precisely to have full control over the serialization. It would probably fill-in your requirements pretty well ! However, it is not ready yet. I estimate that in 1 or 2 weeks I will be able to release a first version.

You can still check the google code : http://code.google.com/p/django-serializable/ , even give some help if you are interested in it.

There will be a featured download when the first release will be out !

sebpiq
Awesome! I've added the RSS feeds to my reader and I'm looking forward to seeing what you put out.
David Wolever
Hello David ! This is not ready yet, because I have been very busy on other things lately, but you can check it out : it can already serialize/deserialize models, and their parents. The only problem being that it is not very stable yet ...
sebpiq