| 1 |
========================== |
|---|
| 2 |
Serializing Django objects |
|---|
| 3 |
========================== |
|---|
| 4 |
|
|---|
| 5 |
.. note:: |
|---|
| 6 |
|
|---|
| 7 |
This API is currently under heavy development and may change -- |
|---|
| 8 |
perhaps drastically -- in the future. |
|---|
| 9 |
|
|---|
| 10 |
You have been warned. |
|---|
| 11 |
|
|---|
| 12 |
Django's serialization framework provides a mechanism for "translating" Django |
|---|
| 13 |
objects into other formats. Usually these other formats will be text-based and |
|---|
| 14 |
used for sending Django objects over a wire, but it's possible for a |
|---|
| 15 |
serializer to handle any format (text-based or not). |
|---|
| 16 |
|
|---|
| 17 |
Serializing data |
|---|
| 18 |
---------------- |
|---|
| 19 |
|
|---|
| 20 |
At the highest level, serializing data is a very simple operation:: |
|---|
| 21 |
|
|---|
| 22 |
from django.core import serializers |
|---|
| 23 |
data = serializers.serialize("xml", SomeModel.objects.all()) |
|---|
| 24 |
|
|---|
| 25 |
The arguments to the ``serialize`` function are the format to serialize the |
|---|
| 26 |
data to (see `Serialization formats`_) and a QuerySet_ to serialize. |
|---|
| 27 |
(Actually, the second argument can be any iterator that yields Django objects, |
|---|
| 28 |
but it'll almost always be a QuerySet). |
|---|
| 29 |
|
|---|
| 30 |
.. _QuerySet: ../db_api/#retrieving-objects |
|---|
| 31 |
|
|---|
| 32 |
You can also use a serializer object directly:: |
|---|
| 33 |
|
|---|
| 34 |
xml_serializer = serializers.get_serializer("xml") |
|---|
| 35 |
xml_serializer.serialize(queryset) |
|---|
| 36 |
data = xml_serializer.getvalue() |
|---|
| 37 |
|
|---|
| 38 |
This is useful if you want to serialize data directly to a file-like object |
|---|
| 39 |
(which includes a HTTPResponse_):: |
|---|
| 40 |
|
|---|
| 41 |
out = open("file.xml", "w") |
|---|
| 42 |
xml_serializer.serialize(SomeModel.objects.all(), stream=out) |
|---|
| 43 |
|
|---|
| 44 |
.. _HTTPResponse: ../request_response/#httpresponse-objects |
|---|
| 45 |
|
|---|
| 46 |
Deserializing data |
|---|
| 47 |
------------------ |
|---|
| 48 |
|
|---|
| 49 |
Deserializing data is also a fairly simple operation:: |
|---|
| 50 |
|
|---|
| 51 |
for obj in serializers.deserialize("xml", data): |
|---|
| 52 |
do_something_with(obj) |
|---|
| 53 |
|
|---|
| 54 |
As you can see, the ``deserialize`` function takes the same format argument as |
|---|
| 55 |
``serialize``, a string or stream of data, and returns an iterator. |
|---|
| 56 |
|
|---|
| 57 |
However, here it gets slightly complicated. The objects returned by the |
|---|
| 58 |
``deserialize`` iterator *aren't* simple Django objects. Instead, they are |
|---|
| 59 |
special ``DeserializedObject`` instances that wrap a created -- but unsaved -- |
|---|
| 60 |
object and any associated relationship data. |
|---|
| 61 |
|
|---|
| 62 |
Calling ``DeserializedObject.save()`` saves the object to the database. |
|---|
| 63 |
|
|---|
| 64 |
This ensures that deserializing is a non-destructive operation even if the |
|---|
| 65 |
data in your serialized representation doesn't match what's currently in the |
|---|
| 66 |
database. Usually, working with these ``DeserializedObject`` instances looks |
|---|
| 67 |
something like:: |
|---|
| 68 |
|
|---|
| 69 |
for deserialized_object in serializers.deserialize("xml", data): |
|---|
| 70 |
if object_should_be_saved(deserialized_object): |
|---|
| 71 |
obj.save() |
|---|
| 72 |
|
|---|
| 73 |
In other words, the usual use is to examine the deserialized objects to make |
|---|
| 74 |
sure that they are "appropriate" for saving before doing so. Of course, if you trust your data source you could just save the object and move on. |
|---|
| 75 |
|
|---|
| 76 |
The Django object itself can be inspected as ``deserialized_object.object``. |
|---|
| 77 |
|
|---|
| 78 |
Serialization formats |
|---|
| 79 |
--------------------- |
|---|
| 80 |
|
|---|
| 81 |
Django "ships" with a few included serializers: |
|---|
| 82 |
|
|---|
| 83 |
========== ============================================================== |
|---|
| 84 |
Identifier Information |
|---|
| 85 |
========== ============================================================== |
|---|
| 86 |
``xml`` Serializes to and from a simple XML dialect. |
|---|
| 87 |
|
|---|
| 88 |
``json`` Serializes to and from JSON_ (using a version of simplejson_ |
|---|
| 89 |
bundled with Django). |
|---|
| 90 |
|
|---|
| 91 |
``python`` Translates to and from "simple" Python objects (lists, dicts, |
|---|
| 92 |
strings, etc.). Not really all that useful on its own, but |
|---|
| 93 |
used as a base for other serializers. |
|---|
| 94 |
========== ============================================================== |
|---|
| 95 |
|
|---|
| 96 |
.. _json: http://json.org/ |
|---|
| 97 |
.. _simplejson: http://undefined.org/python/#simplejson |
|---|
| 98 |
|
|---|
| 99 |
Notes for specific serialization formats |
|---|
| 100 |
---------------------------------------- |
|---|
| 101 |
|
|---|
| 102 |
json |
|---|
| 103 |
~~~~ |
|---|
| 104 |
|
|---|
| 105 |
If you're using UTF-8 (or any other non-ASCII encoding) data with the JSON |
|---|
| 106 |
serializer, you must pass ``ensure_ascii=False`` as a parameter to the |
|---|
| 107 |
``serialize()`` call. Otherwise, the output won't be encoded correctly. |
|---|
| 108 |
|
|---|
| 109 |
For example:: |
|---|
| 110 |
|
|---|
| 111 |
json_serializer = serializers.get_serializer("json") |
|---|
| 112 |
json_serializer.serialize(queryset, ensure_ascii=False, stream=response) |
|---|
| 113 |
|
|---|
| 114 |
Writing custom serializers |
|---|
| 115 |
`````````````````````````` |
|---|
| 116 |
|
|---|
| 117 |
XXX ... |
|---|