mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-05-19 23:33:43 +03:00
PoC Add JSONBoundField to serializers (Fixes #4999)
Per issue #4999, JSONFields are not rendered properly in the DRF browsable API HTML forms. This patch attempts to fix that behavior by introducing a JSONBoundField helper similar to the NestedBoundField helper.
This commit is contained in:
parent
66e015c5ec
commit
11d8e4df88
|
@ -38,7 +38,8 @@ from rest_framework.utils.field_mapping import (
|
||||||
get_relation_kwargs, get_url_kwargs
|
get_relation_kwargs, get_url_kwargs
|
||||||
)
|
)
|
||||||
from rest_framework.utils.serializer_helpers import (
|
from rest_framework.utils.serializer_helpers import (
|
||||||
BindingDict, BoundField, NestedBoundField, ReturnDict, ReturnList
|
BindingDict, BoundField, JSONBoundField, NestedBoundField, ReturnDict,
|
||||||
|
ReturnList
|
||||||
)
|
)
|
||||||
from rest_framework.validators import (
|
from rest_framework.validators import (
|
||||||
UniqueForDateValidator, UniqueForMonthValidator, UniqueForYearValidator,
|
UniqueForDateValidator, UniqueForMonthValidator, UniqueForYearValidator,
|
||||||
|
@ -521,6 +522,8 @@ class Serializer(BaseSerializer):
|
||||||
error = self.errors.get(key) if hasattr(self, '_errors') else None
|
error = self.errors.get(key) if hasattr(self, '_errors') else None
|
||||||
if isinstance(field, Serializer):
|
if isinstance(field, Serializer):
|
||||||
return NestedBoundField(field, value, error)
|
return NestedBoundField(field, value, error)
|
||||||
|
if isinstance(field, JSONField):
|
||||||
|
return JSONBoundField(field, value, error)
|
||||||
return BoundField(field, value, error)
|
return BoundField(field, value, error)
|
||||||
|
|
||||||
# Include a backlink to the serializer class on return objects.
|
# Include a backlink to the serializer class on return objects.
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import collections
|
import collections
|
||||||
|
import json
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
|
@ -82,6 +83,12 @@ class BoundField(object):
|
||||||
return self.__class__(self._field, value, self.errors, self._prefix)
|
return self.__class__(self._field, value, self.errors, self._prefix)
|
||||||
|
|
||||||
|
|
||||||
|
class JSONBoundField(BoundField):
|
||||||
|
def as_form_field(self):
|
||||||
|
value = json.dumps(self.value)
|
||||||
|
return self.__class__(self._field, value, self.errors, self._prefix)
|
||||||
|
|
||||||
|
|
||||||
class NestedBoundField(BoundField):
|
class NestedBoundField(BoundField):
|
||||||
"""
|
"""
|
||||||
This `BoundField` additionally implements __iter__ and __getitem__
|
This `BoundField` additionally implements __iter__ and __getitem__
|
||||||
|
|
Loading…
Reference in New Issue
Block a user