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
670ac25b
Commit
670ac25b
authored
Feb 07, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow serializers to handle dicts as well as objects. Fixes #447.
parent
8113d661
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
48 additions
and
7 deletions
+48
-7
docs/topics/release-notes.md
+1
-0
rest_framework/fields.py
+17
-4
rest_framework/serializers.py
+3
-3
rest_framework/tests/serializer.py
+27
-0
No files found.
docs/topics/release-notes.md
View file @
670ac25b
...
@@ -29,6 +29,7 @@ You can determine your currently installed version using `pip freeze`:
...
@@ -29,6 +29,7 @@ You can determine your currently installed version using `pip freeze`:
### Master
### Master
*
Added a `post_save()` hook to the generic views.
*
Added a `post_save()` hook to the generic views.
*
Allow serializers to handle dicts as well as objects.
*
Bugfix
:
Fix styling on browsable API login.
*
Bugfix
:
Fix styling on browsable API login.
*
Bugfix
:
Fix issue with deserializing empty to-many relations.
*
Bugfix
:
Fix issue with deserializing empty to-many relations.
*
Bugfix
:
Ensure model field validation is still applied for ModelSerializer subclasses with an custom `.restore_object()` method.
*
Bugfix
:
Ensure model field validation is still applied for ModelSerializer subclasses with an custom `.restore_object()` method.
...
...
rest_framework/fields.py
View file @
670ac25b
...
@@ -30,6 +30,21 @@ def is_simple_callable(obj):
...
@@ -30,6 +30,21 @@ def is_simple_callable(obj):
)
)
def
get_component
(
obj
,
attr_name
):
"""
Given an object, and an attribute name,
return that attribute on the object.
"""
if
isinstance
(
obj
,
dict
):
val
=
obj
[
attr_name
]
else
:
val
=
getattr
(
obj
,
attr_name
)
if
is_simple_callable
(
val
):
return
val
()
return
val
class
Field
(
object
):
class
Field
(
object
):
read_only
=
True
read_only
=
True
creation_counter
=
0
creation_counter
=
0
...
@@ -82,11 +97,9 @@ class Field(object):
...
@@ -82,11 +97,9 @@ class Field(object):
if
self
.
source
:
if
self
.
source
:
value
=
obj
value
=
obj
for
component
in
self
.
source
.
split
(
'.'
):
for
component
in
self
.
source
.
split
(
'.'
):
value
=
getattr
(
value
,
component
)
value
=
get_component
(
value
,
component
)
if
is_simple_callable
(
value
):
value
=
value
()
else
:
else
:
value
=
get
attr
(
obj
,
field_name
)
value
=
get
_component
(
obj
,
field_name
)
return
self
.
to_native
(
value
)
return
self
.
to_native
(
value
)
def
to_native
(
self
,
value
):
def
to_native
(
self
,
value
):
...
...
rest_framework/serializers.py
View file @
670ac25b
...
@@ -325,7 +325,7 @@ class BaseSerializer(Field):
...
@@ -325,7 +325,7 @@ class BaseSerializer(Field):
if
self
.
many
is
not
None
:
if
self
.
many
is
not
None
:
many
=
self
.
many
many
=
self
.
many
else
:
else
:
many
=
hasattr
(
obj
,
'__iter__'
)
and
not
isinstance
(
obj
,
Page
)
many
=
hasattr
(
obj
,
'__iter__'
)
and
not
isinstance
(
obj
,
(
Page
,
dict
)
)
if
many
:
if
many
:
return
[
self
.
to_native
(
item
)
for
item
in
obj
]
return
[
self
.
to_native
(
item
)
for
item
in
obj
]
...
@@ -343,7 +343,7 @@ class BaseSerializer(Field):
...
@@ -343,7 +343,7 @@ class BaseSerializer(Field):
if
self
.
many
is
not
None
:
if
self
.
many
is
not
None
:
many
=
self
.
many
many
=
self
.
many
else
:
else
:
many
=
hasattr
(
data
,
'__iter__'
)
and
not
isinstance
(
data
,
dict
)
many
=
hasattr
(
data
,
'__iter__'
)
and
not
isinstance
(
data
,
(
Page
,
dict
)
)
# TODO: error data when deserializing lists
# TODO: error data when deserializing lists
if
many
:
if
many
:
...
@@ -368,7 +368,7 @@ class BaseSerializer(Field):
...
@@ -368,7 +368,7 @@ class BaseSerializer(Field):
if
self
.
many
is
not
None
:
if
self
.
many
is
not
None
:
many
=
self
.
many
many
=
self
.
many
else
:
else
:
many
=
hasattr
(
obj
,
'__iter__'
)
and
not
isinstance
(
obj
,
Page
)
many
=
hasattr
(
obj
,
'__iter__'
)
and
not
isinstance
(
obj
,
(
Page
,
dict
)
)
if
many
:
if
many
:
self
.
_data
=
[
self
.
to_native
(
item
)
for
item
in
obj
]
self
.
_data
=
[
self
.
to_native
(
item
)
for
item
in
obj
]
...
...
rest_framework/tests/serializer.py
View file @
670ac25b
...
@@ -185,6 +185,33 @@ class BasicTests(TestCase):
...
@@ -185,6 +185,33 @@ class BasicTests(TestCase):
self
.
assertEquals
(
instance
.
age
,
self
.
person_data
[
'age'
])
self
.
assertEquals
(
instance
.
age
,
self
.
person_data
[
'age'
])
class
DictStyleSerializer
(
serializers
.
Serializer
):
"""
Note that we don't have any `restore_object` method, so the default
case of simply returning a dict will apply.
"""
email
=
serializers
.
EmailField
()
class
DictStyleSerializerTests
(
TestCase
):
def
test_dict_style_deserialize
(
self
):
"""
Ensure serializers can deserialize into a dict.
"""
data
=
{
'email'
:
'foo@example.com'
}
serializer
=
DictStyleSerializer
(
data
=
data
)
self
.
assertTrue
(
serializer
.
is_valid
())
self
.
assertEquals
(
serializer
.
data
,
data
)
def
test_dict_style_serialize
(
self
):
"""
Ensure serializers can serialize dict objects.
"""
data
=
{
'email'
:
'foo@example.com'
}
serializer
=
DictStyleSerializer
(
data
)
self
.
assertEquals
(
serializer
.
data
,
data
)
class
ValidationTests
(
TestCase
):
class
ValidationTests
(
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
comment
=
Comment
(
self
.
comment
=
Comment
(
...
...
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