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
82de0cf5
Commit
82de0cf5
authored
Jun 21, 2011
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Decimals are a protected_type - let's leave them up to the renderer to deal with
parent
f02a4e18
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
10 additions
and
13 deletions
+10
-13
djangorestframework/renderers.py
+2
-0
djangorestframework/serializer.py
+7
-12
djangorestframework/tests/serializer.py
+1
-1
No files found.
djangorestframework/renderers.py
View file @
82de0cf5
...
...
@@ -108,6 +108,7 @@ class XMLRenderer(BaseRenderer):
"""
Renderer which serializes to XML.
"""
media_type
=
'application/xml'
def
render
(
self
,
obj
=
None
,
media_type
=
None
):
...
...
@@ -251,6 +252,7 @@ class DocumentingTemplateRenderer(BaseRenderer):
The context used in the template contains all the information
needed to self-document the response to this request.
"""
content
=
self
.
_get_content
(
self
.
view
,
self
.
view
.
request
,
obj
,
media_type
)
put_form_instance
=
self
.
_get_form_instance
(
self
.
view
,
'put'
)
...
...
djangorestframework/serializer.py
View file @
82de0cf5
...
...
@@ -4,7 +4,7 @@ Customizable serialization.
from
django.db
import
models
from
django.db.models.query
import
QuerySet
from
django.db.models.fields.related
import
RelatedField
from
django.utils.encoding
import
smart_unicode
from
django.utils.encoding
import
smart_unicode
,
is_protected_type
import
decimal
import
inspect
...
...
@@ -273,13 +273,6 @@ class Serializer(object):
return
self
.
serialize_iter
(
obj
.
all
())
def
serialize_decimal
(
self
,
obj
):
"""
Convert a Decimal instance into a serializable representation.
"""
return
str
(
obj
)
def
serialize_fallback
(
self
,
obj
):
"""
Convert any unhandled object into a serializable representation.
...
...
@@ -301,9 +294,6 @@ class Serializer(object):
elif
isinstance
(
obj
,
models
.
Manager
):
# Manager objects
return
self
.
serialize_manager
(
obj
)
elif
isinstance
(
obj
,
decimal
.
Decimal
):
# Decimals (force to string representation)
return
self
.
serialize_decimal
(
obj
)
elif
inspect
.
isfunction
(
obj
)
and
not
inspect
.
getargspec
(
obj
)[
0
]:
# function with no args
return
self
.
serialize_func
(
obj
)
...
...
@@ -311,5 +301,10 @@ class Serializer(object):
# bound method
return
self
.
serialize_func
(
obj
)
# fall back to smart unicode
# Protected types are passed through as is.
# (i.e. Primitives like None, numbers, dates, and Decimals.)
if
is_protected_type
(
obj
):
return
obj
# All other values are converted to string.
return
self
.
serialize_fallback
(
obj
)
djangorestframework/tests/serializer.py
View file @
82de0cf5
...
...
@@ -18,7 +18,7 @@ class TestObjectToData(TestCase):
def
test_decimal
(
self
):
"""Decimals need to be converted to a string representation."""
self
.
assertEquals
(
self
.
serialize
(
decimal
.
Decimal
(
'1.5'
)),
'1.5'
)
self
.
assertEquals
(
self
.
serialize
(
decimal
.
Decimal
(
'1.5'
)),
decimal
.
Decimal
(
'1.5'
)
)
def
test_function
(
self
):
"""Functions with no arguments should be called."""
...
...
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