Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
80d0ee56
Commit
80d0ee56
authored
Apr 27, 2017
by
Tom Christie
Committed by
GitHub
Apr 27, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5042 from boxingwizards/i4999
PoC Add JSONBoundField to serializers (Fixes #4999)
parents
5ba2368f
f56b1170
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
17 additions
and
3 deletions
+17
-3
rest_framework/serializers.py
+4
-1
rest_framework/utils/field_mapping.py
+2
-2
rest_framework/utils/serializer_helpers.py
+11
-0
No files found.
rest_framework/serializers.py
View file @
80d0ee56
...
...
@@ -38,7 +38,8 @@ from rest_framework.utils.field_mapping import (
get_relation_kwargs
,
get_url_kwargs
)
from
rest_framework.utils.serializer_helpers
import
(
BindingDict
,
BoundField
,
NestedBoundField
,
ReturnDict
,
ReturnList
BindingDict
,
BoundField
,
JSONBoundField
,
NestedBoundField
,
ReturnDict
,
ReturnList
)
from
rest_framework.validators
import
(
UniqueForDateValidator
,
UniqueForMonthValidator
,
UniqueForYearValidator
,
...
...
@@ -521,6 +522,8 @@ class Serializer(BaseSerializer):
error
=
self
.
errors
.
get
(
key
)
if
hasattr
(
self
,
'_errors'
)
else
None
if
isinstance
(
field
,
Serializer
):
return
NestedBoundField
(
field
,
value
,
error
)
if
isinstance
(
field
,
JSONField
):
return
JSONBoundField
(
field
,
value
,
error
)
return
BoundField
(
field
,
value
,
error
)
# Include a backlink to the serializer class on return objects.
...
...
rest_framework/utils/field_mapping.py
View file @
80d0ee56
...
...
@@ -8,7 +8,7 @@ from django.core import validators
from
django.db
import
models
from
django.utils.text
import
capfirst
from
rest_framework.compat
import
DecimalValidator
from
rest_framework.compat
import
DecimalValidator
,
JSONField
from
rest_framework.validators
import
UniqueValidator
NUMERIC_FIELD_TYPES
=
(
...
...
@@ -88,7 +88,7 @@ def get_field_kwargs(field_name, model_field):
if
decimal_places
is
not
None
:
kwargs
[
'decimal_places'
]
=
decimal_places
if
isinstance
(
model_field
,
models
.
TextField
):
if
isinstance
(
model_field
,
models
.
TextField
)
or
(
JSONField
and
isinstance
(
model_field
,
JSONField
))
:
kwargs
[
'style'
]
=
{
'base_template'
:
'textarea.html'
}
if
isinstance
(
model_field
,
models
.
AutoField
)
or
not
model_field
.
editable
:
...
...
rest_framework/utils/serializer_helpers.py
View file @
80d0ee56
from
__future__
import
unicode_literals
import
collections
import
json
from
collections
import
OrderedDict
from
django.utils.encoding
import
force_text
...
...
@@ -82,6 +83,16 @@ class BoundField(object):
return
self
.
__class__
(
self
.
_field
,
value
,
self
.
errors
,
self
.
_prefix
)
class
JSONBoundField
(
BoundField
):
def
as_form_field
(
self
):
value
=
self
.
value
try
:
value
=
json
.
dumps
(
self
.
value
,
sort_keys
=
True
,
indent
=
4
)
except
TypeError
:
pass
return
self
.
__class__
(
self
.
_field
,
value
,
self
.
errors
,
self
.
_prefix
)
class
NestedBoundField
(
BoundField
):
"""
This `BoundField` additionally implements __iter__ and __getitem__
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment