2006-06-28 16:00:37 +00:00
|
|
|
==========================
|
|
|
|
Serializing Django objects
|
|
|
|
==========================
|
|
|
|
|
|
|
|
.. note::
|
2006-09-25 17:44:07 +00:00
|
|
|
|
2006-06-28 16:00:37 +00:00
|
|
|
This API is currently under heavy development and may change --
|
|
|
|
perhaps drastically -- in the future.
|
2006-09-25 17:44:07 +00:00
|
|
|
|
2006-06-28 16:00:37 +00:00
|
|
|
You have been warned.
|
2006-09-25 17:44:07 +00:00
|
|
|
|
2006-06-28 16:00:37 +00:00
|
|
|
Django's serialization framework provides a mechanism for "translating" Django
|
|
|
|
objects into other formats. Usually these other formats will be text-based and
|
|
|
|
used for sending Django objects over a wire, but it's possible for a
|
|
|
|
serializer to handle any format (text-based or not).
|
|
|
|
|
|
|
|
Serializing data
|
|
|
|
----------------
|
|
|
|
|
|
|
|
At the highest level, serializing data is a very simple operation::
|
|
|
|
|
|
|
|
from django.core import serializers
|
|
|
|
data = serializers.serialize("xml", SomeModel.objects.all())
|
2006-09-25 17:44:07 +00:00
|
|
|
|
2006-06-28 16:00:37 +00:00
|
|
|
The arguments to the ``serialize`` function are the format to serialize the
|
|
|
|
data to (see `Serialization formats`_) and a QuerySet_ to serialize.
|
|
|
|
(Actually, the second argument can be any iterator that yields Django objects,
|
|
|
|
but it'll almost always be a QuerySet).
|
|
|
|
|
2007-04-24 05:58:03 +00:00
|
|
|
.. _QuerySet: ../db-api/#retrieving-objects
|
2006-06-28 16:00:37 +00:00
|
|
|
|
|
|
|
You can also use a serializer object directly::
|
|
|
|
|
2007-03-22 12:54:23 +00:00
|
|
|
XMLSerializer = serializers.get_serializer("xml")
|
|
|
|
xml_serializer = XMLSerializer()
|
2006-06-28 16:00:37 +00:00
|
|
|
xml_serializer.serialize(queryset)
|
|
|
|
data = xml_serializer.getvalue()
|
2006-09-25 17:44:07 +00:00
|
|
|
|
2006-06-28 16:00:37 +00:00
|
|
|
This is useful if you want to serialize data directly to a file-like object
|
|
|
|
(which includes a HTTPResponse_)::
|
|
|
|
|
|
|
|
out = open("file.xml", "w")
|
|
|
|
xml_serializer.serialize(SomeModel.objects.all(), stream=out)
|
|
|
|
|
|
|
|
.. _HTTPResponse: ../request_response/#httpresponse-objects
|
|
|
|
|
|
|
|
Deserializing data
|
|
|
|
------------------
|
|
|
|
|
|
|
|
Deserializing data is also a fairly simple operation::
|
|
|
|
|
|
|
|
for obj in serializers.deserialize("xml", data):
|
|
|
|
do_something_with(obj)
|
2006-09-25 17:44:07 +00:00
|
|
|
|
2006-06-28 16:00:37 +00:00
|
|
|
As you can see, the ``deserialize`` function takes the same format argument as
|
|
|
|
``serialize``, a string or stream of data, and returns an iterator.
|
|
|
|
|
|
|
|
However, here it gets slightly complicated. The objects returned by the
|
|
|
|
``deserialize`` iterator *aren't* simple Django objects. Instead, they are
|
|
|
|
special ``DeserializedObject`` instances that wrap a created -- but unsaved --
|
|
|
|
object and any associated relationship data.
|
|
|
|
|
|
|
|
Calling ``DeserializedObject.save()`` saves the object to the database.
|
|
|
|
|
|
|
|
This ensures that deserializing is a non-destructive operation even if the
|
|
|
|
data in your serialized representation doesn't match what's currently in the
|
|
|
|
database. Usually, working with these ``DeserializedObject`` instances looks
|
|
|
|
something like::
|
|
|
|
|
|
|
|
for deserialized_object in serializers.deserialize("xml", data):
|
|
|
|
if object_should_be_saved(deserialized_object):
|
|
|
|
obj.save()
|
2006-09-25 17:44:07 +00:00
|
|
|
|
2006-06-28 16:00:37 +00:00
|
|
|
In other words, the usual use is to examine the deserialized objects to make
|
|
|
|
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.
|
|
|
|
|
|
|
|
The Django object itself can be inspected as ``deserialized_object.object``.
|
|
|
|
|
|
|
|
Serialization formats
|
|
|
|
---------------------
|
|
|
|
|
2006-06-29 16:42:49 +00:00
|
|
|
Django "ships" with a few included serializers:
|
2006-06-28 16:00:37 +00:00
|
|
|
|
2006-06-29 16:42:49 +00:00
|
|
|
========== ==============================================================
|
|
|
|
Identifier Information
|
|
|
|
========== ==============================================================
|
|
|
|
``xml`` Serializes to and from a simple XML dialect.
|
|
|
|
|
|
|
|
``json`` Serializes to and from JSON_ (using a version of simplejson_
|
|
|
|
bundled with Django).
|
|
|
|
|
|
|
|
``python`` Translates to and from "simple" Python objects (lists, dicts,
|
2006-09-25 17:44:07 +00:00
|
|
|
strings, etc.). Not really all that useful on its own, but
|
2006-06-29 16:42:49 +00:00
|
|
|
used as a base for other serializers.
|
|
|
|
========== ==============================================================
|
|
|
|
|
|
|
|
.. _json: http://json.org/
|
|
|
|
.. _simplejson: http://undefined.org/python/#simplejson
|
|
|
|
|
2006-09-25 17:44:07 +00:00
|
|
|
Notes for specific serialization formats
|
2006-09-22 13:26:07 +00:00
|
|
|
----------------------------------------
|
|
|
|
|
|
|
|
json
|
|
|
|
~~~~
|
|
|
|
|
2006-09-25 17:44:07 +00:00
|
|
|
If you're using UTF-8 (or any other non-ASCII encoding) data with the JSON
|
2006-09-22 13:26:07 +00:00
|
|
|
serializer, you must pass ``ensure_ascii=False`` as a parameter to the
|
2006-09-25 17:44:07 +00:00
|
|
|
``serialize()`` call. Otherwise, the output won't be encoded correctly.
|
2006-09-22 13:26:07 +00:00
|
|
|
|
|
|
|
For example::
|
|
|
|
|
|
|
|
json_serializer = serializers.get_serializer("json")
|
|
|
|
json_serializer.serialize(queryset, ensure_ascii=False, stream=response)
|
|
|
|
|
2006-06-29 16:42:49 +00:00
|
|
|
Writing custom serializers
|
|
|
|
``````````````````````````
|
2006-06-28 16:00:37 +00:00
|
|
|
|
2006-06-29 16:42:49 +00:00
|
|
|
XXX ...
|