summaryrefslogtreecommitdiff
path: root/docs/serialization.txt
diff options
context:
space:
mode:
Diffstat (limited to 'docs/serialization.txt')
-rw-r--r--docs/serialization.txt30
1 files changed, 27 insertions, 3 deletions
diff --git a/docs/serialization.txt b/docs/serialization.txt
index aee1b9a3bb..01afa2708c 100644
--- a/docs/serialization.txt
+++ b/docs/serialization.txt
@@ -27,11 +27,12 @@ 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).
-.. _QuerySet: ../db_api/#retrieving-objects
+.. _QuerySet: ../db-api/#retrieving-objects
You can also use a serializer object directly::
- xml_serializer = serializers.get_serializer("xml")
+ XMLSerializer = serializers.get_serializer("xml")
+ xml_serializer = XMLSerializer()
xml_serializer.serialize(queryset)
data = xml_serializer.getvalue()
@@ -43,6 +44,25 @@ This is useful if you want to serialize data directly to a file-like object
.. _HTTPResponse: ../request_response/#httpresponse-objects
+Subset of fields
+~~~~~~~~~~~~~~~~
+
+If you only want a subset of fields to be serialized, you can
+specify a `fields` argument to the serializer::
+
+ from django.core import serializers
+ data = serializers.serialize('xml', SomeModel.objects.all(), fields=('name','size'))
+
+In this example, only the `name` and `size` attributes of each model will
+be serialized.
+
+.. note::
+
+ Depending on your model, you may find that it is not possible to deserialize
+ a model that only serializes a subset of its fields. If a serialized object
+ doesn't specify all the fields that are required by a model, the deserializer
+ will not be able to save deserialized instances.
+
Deserializing data
------------------
@@ -91,10 +111,14 @@ Django "ships" with a few included serializers:
``python`` Translates to and from "simple" Python objects (lists, dicts,
strings, etc.). Not really all that useful on its own, but
used as a base for other serializers.
+
+ ``yaml`` Serializes to YAML (Yet Another Markup Lanuage). This
+ serializer is only available if PyYAML_ is installed.
========== ==============================================================
.. _json: http://json.org/
.. _simplejson: http://undefined.org/python/#simplejson
+.. _PyYAML: http://www.pyyaml.org/
Notes for specific serialization formats
----------------------------------------
@@ -108,7 +132,7 @@ serializer, you must pass ``ensure_ascii=False`` as a parameter to the
For example::
- json_serializer = serializers.get_serializer("json")
+ json_serializer = serializers.get_serializer("json")()
json_serializer.serialize(queryset, ensure_ascii=False, stream=response)
Writing custom serializers