1
0
mirror of https://github.com/django/django.git synced 2025-10-23 21:59:11 +00:00

Fixed #15091 -- Allowed passing custom encoder to JSON serializer.

This commit is contained in:
Berker Peksag
2016-06-28 14:48:19 +03:00
committed by Tim Graham
parent 6bf7964023
commit c1b6f554e4
4 changed files with 37 additions and 2 deletions

View File

@@ -206,6 +206,10 @@ Serialization
* The new ``django.core.serializers.base.Serializer.stream_class`` attribute
allows subclasses to customize the default stream.
* The encoder used by the :ref:`JSON serializer <serialization-formats-json>`
can now be customized by passing a ``cls`` keyword argument to the
``serializers.serialize()`` function.
Signals
~~~~~~~

View File

@@ -265,6 +265,17 @@ work::
return force_text(obj)
return super(LazyEncoder, self).default(obj)
You can then pass ``cls=LazyEncoder`` to the ``serializers.serialize()``
function::
from django.core.serializers import serialize
serialize('json', SomeModel.objects.all(), cls=LazyEncoder)
.. versionchanged:: 1.11
The ability to use a custom encoder using ``cls=...`` was added.
Also note that GeoDjango provides a :doc:`customized GeoJSON serializer
</ref/contrib/gis/serializers>`.