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
792bc4d6
Commit
792bc4d6
authored
Jan 10, 2012
by
Sébastien Piquemal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed issue#73 and added a test
parent
0d64b4a7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
7 deletions
+18
-7
djangorestframework/serializer.py
+6
-4
djangorestframework/tests/serializer.py
+12
-3
No files found.
djangorestframework/serializer.py
View file @
792bc4d6
...
@@ -230,12 +230,14 @@ class Serializer(object):
...
@@ -230,12 +230,14 @@ class Serializer(object):
# serialize each required field
# serialize each required field
for
fname
in
fields
:
for
fname
in
fields
:
try
:
try
:
if
inspect
.
ismethod
(
getattr
(
self
,
fname
,
None
))
and
\
# we first check for a method 'fname' on self,
len
(
inspect
.
getargspec
(
getattr
(
self
,
fname
))[
0
])
==
2
:
# 'fname's signature must be 'def fname(self, instance)'
# check first for a method 'fname' on self first
meth
=
getattr
(
self
,
fname
,
None
)
if
(
inspect
.
ismethod
(
meth
)
and
len
(
inspect
.
getargspec
(
meth
)[
0
])
==
2
):
obj
=
meth
(
instance
)
obj
=
meth
(
instance
)
elif
hasattr
(
instance
,
'__contains__'
)
and
fname
in
instance
:
elif
hasattr
(
instance
,
'__contains__'
)
and
fname
in
instance
:
# check for a key 'fname' on the instance
#
then
check for a key 'fname' on the instance
obj
=
instance
[
fname
]
obj
=
instance
[
fname
]
elif
hasattr
(
instance
,
smart_str
(
fname
)):
elif
hasattr
(
instance
,
smart_str
(
fname
)):
# finally check for an attribute 'fname' on the instance
# finally check for an attribute 'fname' on the instance
...
...
djangorestframework/tests/serializer.py
View file @
792bc4d6
...
@@ -34,9 +34,7 @@ class TestObjectToData(TestCase):
...
@@ -34,9 +34,7 @@ class TestObjectToData(TestCase):
self
.
assertEquals
(
self
.
serialize
(
Foo
()
.
foo
),
1
)
self
.
assertEquals
(
self
.
serialize
(
Foo
()
.
foo
),
1
)
def
test_datetime
(
self
):
def
test_datetime
(
self
):
"""
"""datetime objects are left as-is."""
datetime objects are left as-is.
"""
now
=
datetime
.
datetime
.
now
()
now
=
datetime
.
datetime
.
now
()
self
.
assertEquals
(
self
.
serialize
(
now
),
now
)
self
.
assertEquals
(
self
.
serialize
(
now
),
now
)
...
@@ -121,3 +119,14 @@ class TestFieldNesting(TestCase):
...
@@ -121,3 +119,14 @@ class TestFieldNesting(TestCase):
self
.
assertEqual
(
SerializerM2
()
.
serialize
(
self
.
m2
),
{
'field'
:
{
'field1'
:
u'foo'
}})
self
.
assertEqual
(
SerializerM2
()
.
serialize
(
self
.
m2
),
{
'field'
:
{
'field1'
:
u'foo'
}})
self
.
assertEqual
(
SerializerM3
()
.
serialize
(
self
.
m3
),
{
'field'
:
{
'field2'
:
u'bar'
}})
self
.
assertEqual
(
SerializerM3
()
.
serialize
(
self
.
m3
),
{
'field'
:
{
'field2'
:
u'bar'
}})
def
test_serializer_unvalid_hook_method
(
self
):
"""
Test serializing a model instance with an unvalid hook method on the serializer.
"""
class
SerializerM2
(
Serializer
):
fields
=
(
'unvalid_hook'
,
)
def
unvalid_hook
(
self
):
return
self
.
m2
.
unvalid_hook
=
'bla'
self
.
assertEqual
(
SerializerM2
()
.
serialize_model
(
self
.
m2
),
{
'unvalid_hook'
:
'bla'
})
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