mirror of
https://github.com/django/django.git
synced 2025-08-04 19:08:28 +00:00
Refs #27795 -- Replaced many force_text() with str()
Thanks Tim Graham for the review.
This commit is contained in:
parent
8ab7ce8558
commit
301de774c2
47 changed files with 135 additions and 181 deletions
|
@ -256,13 +256,12 @@ For example, if you have some custom type in an object to be serialized, you'll
|
|||
have to write a custom :mod:`json` encoder for it. Something like this will
|
||||
work::
|
||||
|
||||
from django.utils.encoding import force_text
|
||||
from django.core.serializers.json import DjangoJSONEncoder
|
||||
|
||||
class LazyEncoder(DjangoJSONEncoder):
|
||||
def default(self, obj):
|
||||
if isinstance(obj, YourCustomType):
|
||||
return force_text(obj)
|
||||
return str(obj)
|
||||
return super().default(obj)
|
||||
|
||||
You can then pass ``cls=LazyEncoder`` to the ``serializers.serialize()``
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue