You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Two slightly different iso 8601 datetime serialization #4255
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.
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 Responsebefore 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.
Activity
georgejlee commentedon Jul 21, 2016
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.
ephes commentedon Jul 22, 2016
@georgejlee I use a custom renderer to work around this:
Hmm, changing DEFAULT_RENDERER_CLASSES to custom renderer did not affect the rendering of my viewsets. Had to set renderer_class explicitly, strange.
saschwarz commentedon Jan 24, 2021
In case anyone else stumbles across this while trying to configure DRF to use a custom encoder via
encoder_classfordatetime. 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'sDateTimeFieldhas itsformatkwarg set toNoneOR, if you don't specifyDateTimeFieldserializers, then you need to setREST_FRAMEWORK's config forDEFAULT_FORMAT: Nonein yoursettings.py. Only then will thecustom encoderbe used.The reason is, the serializer field(s) (or default field renderer) will be used to format the
Responsebefore the customRendereris used to render thedatetime. 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'sdefaultmethod won't be called.