Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set DatetimeField format of django rest framework configuration

Tags:

People also ask

What is the format of Datetimefield in Django?

DATETIME_FORMAT="%Y-%m-%d%H:%M:%S"

What is Serializers SerializerMethodField?

SerializerMethodField. This is a read-only field. It gets its value by calling a method on the serializer class it is attached to. It can be used to add any sort of data to the serialized representation of your object.

What is Default_authentication_classes?

DEFAULT_AUTHENTICATION_CLASSES. A list or tuple of authentication classes, that determines the default set of authenticators used when accessing the request.

What are Serializers in Django REST framework?

Serializers in Django REST Framework are responsible for converting objects into data types understandable by javascript and front-end frameworks. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data.


I try to use DRF serializers to serialize a model object. I find the DatetimeField in object won't output "2015-10-21T09:28:53.769000Z" of the ISO-8601 format

I lookup DRF document why I can't output ISO-8601 format. According to datetimefield says:

format - A string representing the output format. If not specified, this defaults to the same value as the DATETIME_FORMAT settings key, which will be 'iso-8601' unless set. Setting to a format string indicates that to_representation return values should be coerced to string output. Format strings are described below. Setting this value to None indicates that Python

It means It's default to output iso-8601 format if I never set DATETIME_FORMAT argument? Not yet, it's still no change.

When I try to write setting of django project as the following:

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

or I write in DatetimeField argument as the following:

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

It's still no change again.

Anyone know how to set it?


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!