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
6b4bb48d
Commit
6b4bb48d
authored
Mar 12, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial support for writable nested serialization (Not ModelSerializer)
parent
20880232
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
115 additions
and
17 deletions
+115
-17
rest_framework/serializers.py
+53
-17
rest_framework/tests/serializer_nested.py
+62
-0
No files found.
rest_framework/serializers.py
View file @
6b4bb48d
...
...
@@ -20,6 +20,11 @@ from rest_framework.relations import *
from
rest_framework.fields
import
*
class
NestedValidationError
(
ValidationError
):
def
__init__
(
self
,
message
):
self
.
messages
=
message
class
DictWithMetadata
(
dict
):
"""
A dict-like object, that can have additional properties attached.
...
...
@@ -98,7 +103,7 @@ class SerializerOptions(object):
self
.
exclude
=
getattr
(
meta
,
'exclude'
,
())
class
BaseSerializer
(
Field
):
class
BaseSerializer
(
Writable
Field
):
"""
This is the Serializer implementation.
We need to implement it as `BaseSerializer` due to metaclass magicks.
...
...
@@ -303,33 +308,64 @@ class BaseSerializer(Field):
return
self
.
to_native
(
obj
)
try
:
if
self
.
source
:
for
component
in
self
.
source
.
split
(
'.'
):
obj
=
getattr
(
obj
,
component
)
if
is_simple_callable
(
obj
):
obj
=
obj
()
else
:
obj
=
getattr
(
obj
,
field_name
)
if
is_simple_callable
(
obj
):
obj
=
obj
()
source
=
self
.
source
or
field_name
value
=
obj
for
component
in
source
.
split
(
'.'
):
value
=
get_component
(
value
,
component
)
if
value
is
None
:
break
except
ObjectDoesNotExist
:
return
None
# If the object has an "all" method, assume it's a relationship
if
is_simple_callable
(
getattr
(
obj
,
'all'
,
None
)):
return
[
self
.
to_native
(
item
)
for
item
in
obj
.
all
()]
if
is_simple_callable
(
getattr
(
value
,
'all'
,
None
)):
return
[
self
.
to_native
(
item
)
for
item
in
value
.
all
()]
if
obj
is
None
:
if
value
is
None
:
return
None
if
self
.
many
is
not
None
:
many
=
self
.
many
else
:
many
=
hasattr
(
obj
,
'__iter__'
)
and
not
isinstance
(
obj
,
(
Page
,
dict
,
six
.
text_type
))
many
=
hasattr
(
value
,
'__iter__'
)
and
not
isinstance
(
value
,
(
Page
,
dict
,
six
.
text_type
))
if
many
:
return
[
self
.
to_native
(
item
)
for
item
in
obj
]
return
self
.
to_native
(
obj
)
return
[
self
.
to_native
(
item
)
for
item
in
value
]
return
self
.
to_native
(
value
)
def
field_from_native
(
self
,
data
,
files
,
field_name
,
into
):
if
self
.
read_only
:
return
try
:
value
=
data
[
field_name
]
except
KeyError
:
if
self
.
required
:
raise
ValidationError
(
self
.
error_messages
[
'required'
])
return
if
self
.
parent
.
object
:
# Set the serializer object if it exists
obj
=
getattr
(
self
.
parent
.
object
,
field_name
)
self
.
object
=
obj
if
value
in
(
None
,
''
):
into
[(
self
.
source
or
field_name
)]
=
None
else
:
kwargs
=
{
'data'
:
value
,
'context'
:
self
.
context
,
'partial'
:
self
.
partial
,
'many'
:
self
.
many
}
serializer
=
self
.
__class__
(
**
kwargs
)
if
serializer
.
is_valid
():
self
.
object
=
serializer
.
object
into
[
self
.
source
or
field_name
]
=
serializer
.
object
else
:
# Propagate errors up to our parent
raise
NestedValidationError
(
serializer
.
errors
)
@property
def
errors
(
self
):
...
...
rest_framework/tests/serializer_nested.py
0 → 100644
View file @
6b4bb48d
from
__future__
import
unicode_literals
from
django.test
import
TestCase
from
rest_framework
import
serializers
class
TrackSerializer
(
serializers
.
Serializer
):
order
=
serializers
.
IntegerField
()
title
=
serializers
.
CharField
(
max_length
=
100
)
duration
=
serializers
.
IntegerField
()
class
AlbumSerializer
(
serializers
.
Serializer
):
album_name
=
serializers
.
CharField
(
max_length
=
100
)
artist
=
serializers
.
CharField
(
max_length
=
100
)
tracks
=
TrackSerializer
(
many
=
True
)
class
NestedSerializerTestCase
(
TestCase
):
def
test_nested_validation_success
(
self
):
"""
Correct nested serialization should return the input data.
"""
data
=
{
'album_name'
:
'Discovery'
,
'artist'
:
'Daft Punk'
,
'tracks'
:
[
{
'order'
:
1
,
'title'
:
'One More Time'
,
'duration'
:
235
},
{
'order'
:
2
,
'title'
:
'Aerodynamic'
,
'duration'
:
184
},
{
'order'
:
3
,
'title'
:
'Digital Love'
,
'duration'
:
239
}
]
}
serializer
=
AlbumSerializer
(
data
=
data
)
self
.
assertEqual
(
serializer
.
is_valid
(),
True
)
self
.
assertEqual
(
serializer
.
data
,
data
)
def
test_nested_validation_error
(
self
):
"""
Incorrect nested serialization should return appropriate error data.
"""
data
=
{
'album_name'
:
'Discovery'
,
'artist'
:
'Daft Punk'
,
'tracks'
:
[
{
'order'
:
1
,
'title'
:
'One More Time'
,
'duration'
:
235
},
{
'order'
:
2
,
'title'
:
'Aerodynamic'
,
'duration'
:
184
},
{
'order'
:
3
,
'title'
:
'Digital Love'
,
'duration'
:
'foobar'
}
]
}
expected_errors
=
{
'tracks'
:
[
{},
{},
{
'duration'
:
[
'Enter a whole number.'
]}
]
}
serializer
=
AlbumSerializer
(
data
=
data
)
self
.
assertEqual
(
serializer
.
is_valid
(),
False
)
self
.
assertEqual
(
serializer
.
errors
,
expected_errors
)
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