Django: custom serialization options?

百般思念 提交于 2019-12-09 06:46:52

问题


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.


回答1:


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.)




回答2:


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...




回答3:


EDIT :

Now out at https://bitbucket.org/sebpiq/any2any/


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 !



来源:https://stackoverflow.com/questions/3055650/django-custom-serialization-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!