[Fixed]-Set DatetimeField format of django rest framework configuration

32👍

If you don’t konw what happen and you still don’t slove that, I can define a datettime format in setting as the following:

REST_FRAMEWORK = {
    'DATETIME_FORMAT': "%Y-%m-%dT%H:%M:%S.%fZ",
}

4👍

You don’t need to define DATETIME_FORMAT in settings or format in last_login field as iso-861 is the default format.

Here is a sample example showing the serialized output of a datetime field in iso-861 format.

In [1]: from rest_framework import serializers

In [2]: from datetime import datetime

In [3]: class SomeSerializer(serializers.Serializer):
   ....:     last_login = serializers.DateTimeField()
   ....:     

In [4]: x = SomeSerializer(data={'last_login':datetime.now()})

In [5]: x.is_valid()
Out[5]: True

In [6]: x.data # representation of 'last_login' will be in iso-8601 formatted string
Out[6]: OrderedDict([('last_login', u'2015-10-22T09:32:02.788611Z')])

Leave a comment