I am quite interested in this, as most of my coding work has been 
developing APIs that get consumed by non-web-browser software.

Lately, I've taken the approach that a Form is the appropriate tool to use 
for (de)serialisation: it's already used extensively by the template 
rendering (which is indeed serialisation of data).

The workflow I use is:

Model instance -> Model form -> pre-serialised (python) data structure -> 
serialised data

That way, the only bits of code that are required are the pre-serialiser: 
which takes a Form object (possibly a ModelForm, but not necessarily), and 
returns a python dict. This handles the nested pre-serialisation, for 
instance.

Then, for each serialisation type, you need a serialiser that converts that 
dict into the relevant type. For vanilla JSON, this is simply json.dumps(), 
and it is trivial to get a simple XML output too.


As for going back the other way: the data that comes in can be 
deserialised, and then passed directly to the form(), just as you would 
with request.POST.


Matt.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-developers/-/12mcu7EJmDQJ.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to