Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django datetime format different from DRF serializer datetime format

I am trying to understand why this is happening. I have a Django DateTime field and Django Rest Framework serializer that uses the field.

I am trying to compare the dates for both of them and get the following results from JSON endpoint and model result:

DRF: 2018-12-21T19:17:59.353368Z
Model field: 2018-12-21T19:17:59.353368+00:00

Is there a way to make them similar? So, either to make both of them be "Z" or "+00:00."

like image 778
Gasim Avatar asked Dec 21 '18 19:12

Gasim


People also ask

What is the format of datetime in Django?

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

What is difference between ModelSerializer and serializer?

The ModelSerializer class is the same as a regular Serializer class, except that: It will automatically generate a set of fields for you, based on the model. It will automatically generate validators for the serializer, such as unique_together validators. It includes simple default implementations of .

Is it necessary to use Serializers in Django?

It is not necessary to use a serializer. You can do what you would like to achieve in a view. However, serializers help you a lot. If you don't want to use serializer, you can inherit APIView at a function-based-view.

What are different types of serializer in Django rest?

The serializers in REST framework work very similarly to Django's Form and ModelForm classes. The two major serializers that are most popularly used are ModelSerializer and HyperLinkedModelSerialzer.

What are date and time fields in serializers in Django REST framework?

This article revolves around Date and time Fields in Serializers in Django REST Framework. There are four major fields – DateTimeField, DateField, TimeField and DurationField. DateTimeField is a serializer field used for date and time representation.

What is datetime_format in Django?

It is same as – DateTimeField – Django Models 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.

What is the datetimefield format for a DRF serializer?

So, borrowing from @RezaTorkamanAhmadi's answer, for anyone looking to get a DRF serializer DateTimeField to have the format 2018-12-21T19:17:59.353368+00:00 (the same format as the default models.DateTimeField so that your serialized values match your model values -- the OP's question and mine too) you're looking for either:

What is serializing in Django?

In Django REST Framework the very concept of Serializing is to convert DB data to a datatype that can be used by javascript. Every serializer comes with some fields (entries) which are going to be processed. For example if you have a class with name Employee and its fields as Employee_id, Employee_name, date, etc.


1 Answers

It's because django rest framework uses it's own datetime formating. To change that, in your settings.py file, there should exist a dict variable called REST_FRAMEWORK (if not create it) and add this:

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

Also check USE_TZ variable state too in your settings.py

like image 109
Reza Torkaman Ahmadi Avatar answered Sep 21 '22 01:09

Reza Torkaman Ahmadi