Skip to content

Two slightly different iso 8601 datetime serialization #4255

Closed

Description

@sebdiem

Checklist

  • I have verified that that issue exists against the master branch of Django REST framework.
  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • This is not a usage question. (Those should be directed to the discussion group instead.)
  • This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • I have reduced the issue to the simplest possible case.
  • I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Steps to reproduce

The implementation of datetime serialization in ISO-8601 format is not consistent in the codebase.
In https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/fields.py#L1094 microseconds are included in the serialized value
In https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/utils/encoders.py#L30 only milliseconds are

Expected behavior

I would expect a consistent implementation.

Actual behavior

N/A

Activity

  1. added this to the 3.4.0 Release milestone on Jul 11, 2016
  2. georgejlee commented on Jul 21, 2016

    @georgejlee

    Currently some clients of my API require dates with millisecond precision and cannot handle microseconds. I achieved this by setting 'DATETIME_FORMAT' to None in the settings for DRF 3.3. Upgrading to 3.4 breaks this behavior. Is there an easy way to get the previous behavior? I can't figure out how to specify a datetime format string that gives me the ECMA-262 format. Thanks.

  3. ephes commented on Jul 22, 2016

    @ephes
    Contributor

    @georgejlee I use a custom renderer to work around this:

    import datetime
    
    from rest_framework.renderers import JSONRenderer
    from rest_framework.utils.encoders import JSONEncoder
    
    class MilliSecondEncoder(JSONEncoder):
        def default(self, obj):
            if isinstance(obj, datetime.datetime):
                representation = obj.isoformat()
                if obj.microsecond:
                    representation = representation[:23] + representation[26:]
                if representation.endswith('+00:00'):
                    representation = representation[:-6] + 'Z'
                return representation
            else:
                return super().default(obj)
    
    
    class JSONRenderer(JSONRenderer):
        encoder_class = MilliSecondEncoder
    

    Hmm, changing DEFAULT_RENDERER_CLASSES to custom renderer did not affect the rendering of my viewsets. Had to set renderer_class explicitly, strange.

  4. saschwarz commented on Jan 24, 2021

    @saschwarz

    In case anyone else stumbles across this while trying to configure DRF to use a custom encoder via encoder_class for datetime. The issue that kept the solution in #4255 (comment) from working for me was DRF will use its built-in encoder unless either the field's DateTimeField has its format kwarg set to None OR, if you don't specify DateTimeField serializers, then you need to set REST_FRAMEWORK's config for DEFAULT_FORMAT: None in your settings.py. Only then will the custom encoder be used.

    The reason is, the serializer field(s) (or default field renderer) will be used to format the Response before the custom Renderer is used to render the datetime. So the datetime field will already be a string and the default JSONEncoder won't invoke the custom encoder, since the default Python types are handled by the encoder so the custom encoder's default method won't be called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions