Skip to content

Commit 4fc3b1b

Browse files
author
James Cleveland
committed
Add timedelta encoder to the JSONEncoder class.
Whilst this commit adds *encoding* of timedeltas to a string of a floating point value of the seconds, you must add your own serializer field for whatever timedelta model field you are using. This is because Django doesn't support any kind of timedelta field out-of-the-box, so you have to either implement your own or use django-timedelta. If this is the case and you want to serialise timedelta input, you will have to implement your own special field to use for the timedelta, which is not included in core as it is based on a 3rd party library. Here is an example: import datetime import timedelta from django import forms from django.core import validators from django.core.exceptions import ValidationError from django.utils.translation import ugettext_lazy as _ from rest_framework.fields import WritableField class TimedeltaField(WritableField): type_name = 'TimedeltaField' form_field_class = forms.FloatField default_error_messages = { 'invalid': _("'%s' value must be in seconds."), } def from_native(self, value): if value in validators.EMPTY_VALUES: return None try: return datetime.timedelta(seconds=float(value)) except (TypeError, ValueError): msg = self.error_messages['invalid'] % value raise ValidationError(msg) Which is based on the FloatField. This field can then be used in your serializer like this: from yourapp.fields import TimedeltaField class YourSerializer(serializers.ModelSerializer): duration = TimedeltaField()
1 parent da6b957 commit 4fc3b1b

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

rest_framework/utils/encoders.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class JSONEncoder(json.JSONEncoder):
1414
"""
15-
JSONEncoder subclass that knows how to encode date/time,
15+
JSONEncoder subclass that knows how to encode date/time/timedelta,
1616
decimal types, and generators.
1717
"""
1818
def default(self, o):
@@ -34,6 +34,8 @@ def default(self, o):
3434
if o.microsecond:
3535
r = r[:12]
3636
return r
37+
elif isinstance(o, datetime.timedelta):
38+
return str(o.total_seconds())
3739
elif isinstance(o, decimal.Decimal):
3840
return str(o)
3941
elif hasattr(o, '__iter__'):

0 commit comments

Comments
 (0)