django-rest-framework/rest_framework
James Cleveland 4fc3b1ba56 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()
2013-01-15 13:08:52 +00:00
..
authtoken ObtainAuthToken pluggable Serializer. 2013-01-08 12:20:01 +00:00
runtests Cleanup runtests/runcoverage 2012-12-30 08:06:11 +00:00
static/rest_framework Make textareas in browseable API same width as everything else 2012-11-07 11:32:52 +00:00
templates/rest_framework missing rest_framework templatetags for statics in login template 2012-12-30 13:24:05 +09:00
templatetags Use the correct static template tag in Django 1.5 2013-01-06 15:49:12 -05:00
tests Update docstrings 2013-01-15 10:51:10 +00:00
utils Add timedelta encoder to the JSONEncoder class. 2013-01-15 13:08:52 +00:00
__init__.py Version 2.1.16 2013-01-14 09:20:44 +00:00
authentication.py Explicit CSRF failure message. Fixes #60. 2012-10-15 14:03:36 +01:00
compat.py Merge branch 'master' of git://github.com/tomchristie/django-rest-framework into patch-support 2012-12-30 14:03:08 -04:00
decorators.py fixed some typos 2012-11-14 19:36:29 +01:00
exceptions.py Fixes for urls with suffixes 2012-10-29 17:08:38 +00:00
fields.py Tweak comment. 2013-01-03 22:06:55 +00:00
filters.py Fixed identation on filter_fields 2012-11-19 00:14:03 +01:00
generics.py Add .patch() method for RetrieveUpdateAPIView 2013-01-02 17:43:43 +00:00
mixins.py Keep API backwards compatible. 2013-01-02 13:39:24 +00:00
models.py Change package name: djangorestframework -> rest_framework 2012-09-20 13:06:27 +01:00
negotiation.py Renderer negotiation: media_type specificty evaluation weak 2012-11-27 10:13:15 +01:00
pagination.py Drop unneeded passing through of kwargs now context issue is resolved. 2012-12-14 20:12:50 +00:00
parsers.py deprecate simplejson 2013-01-05 13:40:02 +01:00
permissions.py Version 2.1.6. AKA: I am a doofus. 2012-11-23 13:21:18 +00:00
relations.py Merge remote-tracking branch 'upstream/master' into null-one-to-one 2013-01-08 08:33:01 -08:00
renderers.py deprecate simplejson 2013-01-05 13:40:02 +01:00
request.py Added setter to the auth property 2012-12-20 23:48:10 +00:00
response.py Merge pull request #407 from ludwigkraatz/location_header 2012-11-14 10:42:08 -08:00
reverse.py Fixes for urls with suffixes 2012-10-29 17:08:38 +00:00
serializers.py PK fields should only be read-only if they are an AutoField. Fixes #563 2013-01-12 09:43:14 +00:00
settings.py Be more informative when reporting import errors. 2013-01-07 12:52:20 +00:00
status.py fixed typo in html status code 2012-11-06 21:11:05 +01:00
throttling.py Fix some typos. 2012-10-30 00:30:52 +01:00
urlpatterns.py Use compat import of urlpatterns 2012-12-27 20:20:01 +00:00
urls.py urls, patterns, include imports move to compat to support incoming 1.3 thru 1.6 import compatability 2012-12-19 23:12:27 +00:00
views.py fixed some typos 2012-11-14 19:36:29 +01:00