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
5484d570
Commit
5484d570
authored
Jan 19, 2015
by
Fabien Bochu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix timedelta JSON serialization on Python 2.6.
parent
4f27b966
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
10 additions
and
2 deletions
+10
-2
rest_framework/compat.py
+8
-0
rest_framework/utils/encoders.py
+2
-2
No files found.
rest_framework/compat.py
View file @
5484d570
...
...
@@ -33,6 +33,14 @@ def unicode_to_repr(value):
return
value
def
total_seconds
(
timedelta
):
# TimeDelta.total_seconds() is only available in Python 2.7
if
hasattr
(
timedelta
,
'total_seconds'
):
return
timedelta
.
total_seconds
()
else
:
return
(
timedelta
.
days
*
86400.0
)
+
float
(
timedelta
.
seconds
)
+
(
timedelta
.
microseconds
/
1000000.0
)
# OrderedDict only available in Python 2.7.
# This will always be the case in Django 1.7 and above, as these versions
# no longer support Python 2.6.
...
...
rest_framework/utils/encoders.py
View file @
5484d570
...
...
@@ -6,7 +6,7 @@ from django.db.models.query import QuerySet
from
django.utils
import
six
,
timezone
from
django.utils.encoding
import
force_text
from
django.utils.functional
import
Promise
from
rest_framework.compat
import
OrderedDict
from
rest_framework.compat
import
OrderedDict
,
total_seconds
from
rest_framework.utils.serializer_helpers
import
ReturnDict
,
ReturnList
import
datetime
import
decimal
...
...
@@ -41,7 +41,7 @@ class JSONEncoder(json.JSONEncoder):
representation
=
representation
[:
12
]
return
representation
elif
isinstance
(
obj
,
datetime
.
timedelta
):
return
six
.
text_type
(
obj
.
total_seconds
(
))
return
six
.
text_type
(
total_seconds
(
obj
))
elif
isinstance
(
obj
,
decimal
.
Decimal
):
# Serializers will coerce decimals to strings by default.
return
float
(
obj
)
...
...
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