Set the DatetimeField format for django rest platform configuration

I am trying to use DRF serializers to serialize a model object. I believe that the DatetimeField in the object will not output "2015-10-21T09:28:53.769000Z" the ISO-8601 format

I am looking for a DRF document, why I can not output the ISO-8601 format. According to datetimefield says:

format - A string representing the output format. If not specified, this default value matches the value of the DATETIME_FORMAT parameter, which will be "iso-8601" if not set. Setting to a format string indicates that the return values ​​of to_representation should be forced into the string output. Format strings are described below. Setting this value to None means Python

Does this mean that it defaults to the iso-8601 if I never set the DATETIME_FORMAT argument? There are no changes yet.

When I try to write the django project setup as follows:

 REST_FRAMEWORK = { 'DATETIME_FORMAT': "iso-8601", } 

or I write in the DatetimeField argument as follows:

 class UserSerializer(...): last_login = DatetimeField(format='iso-8601') class Meta: model = User fields = ('email', 'displayname', 'is_active', 'date_joined', 'last_login') 

This has not changed yet.

Does anyone know how to install it?

+6
source share
2 answers

If you do not know what will happen and you are still not using this, I can determine the date format in the setting, as shown below:

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

You do not need to specify DATETIME_FORMAT in the settings or format in the last_login field, since the iso-861 format is the default format.

Here is an 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')]) 
+1
source

All Articles