Author: mtredinnick
Date: 2007-11-03 21:08:24 -0500 (Sat, 03 Nov 2007)
New Revision: 6645
Modified:
django/trunk/docs/serialization.txt
Log:
Fixed #5868 -- Provided an example of how to extend simplejson to serialize
lazy translation objects for those who want to use it directly.
Modified: django/trunk/docs/serialization.txt
===================================================================
--- django/trunk/docs/serialization.txt 2007-11-04 02:08:15 UTC (rev 6644)
+++ django/trunk/docs/serialization.txt 2007-11-04 02:08:24 UTC (rev 6645)
@@ -47,14 +47,14 @@
Subset of fields
~~~~~~~~~~~~~~~~
-If you only want a subset of fields to be serialized, you can
+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.
+be serialized.
.. note::
@@ -111,9 +111,9 @@
``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.
+
+ ``yaml`` Serializes to YAML (Yet Another Markup Lanuage). This
+ serializer is only available if PyYAML_ is installed.
========== ==============================================================
.. _json: http://json.org/
@@ -135,6 +135,23 @@
json_serializer = serializers.get_serializer("json")()
json_serializer.serialize(queryset, ensure_ascii=False, stream=response)
+Django ships with a copy of simplejson_ in the source. Be aware, that if
+you're using that for serializing directly that not all Django output can be
+passed unmodified to simplejson. In particular, `lazy translation objects`_
+need a `special encoder`_ written for them. Something like this will work::
+
+ from django.utils.functional import Promise
+ from django.utils.encoding import force_unicode
+
+ class LazyEncoder(simplejson.JSONEncoder):
+ def default(self, obj):
+ if isinstance(obj, Promise):
+ return force_unicode(obj)
+ return obj
+
+.. _lazy translation objects: ../i18n/#lazy-translation
+.. _special encoder:
http://svn.red-bean.com/bob/simplejson/tags/simplejson-1.7/docs/index.html
+
Writing custom serializers
``````````````````````````
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---