5

In Django tutorials, there is a sentence described like below.

TIME_ZONE

...

When USE_TZ is False, this is the time zone in which Django will store all datetimes. When USE_TZ is True, this is the default time zone that Django will use to display datetimes in templates and to interpret datetimes entered in forms.

...

When django rest framework takes the naive datetime data from request. Then Django will interpret this naive datetime to aware local datetime of TIME_ZONE setting? And if it is right, how does it work?

Thanks in advance!

nextdoordoc
  • 1,727
  • 6
  • 20
  • 30

2 Answers2

9

Generally, an input timezone is determined in DRF while parsing the request in the serializer's DateTimeField (similar to form fields).

You can control the format of such input, and there's even a general setting DATETIME_INPUT_FORMATS which defaults to ['iso-8601'].

This basically means that the input can both omit and specify the time zone using the ISO-8601 format and the field will generally be able to determine whether to create an aware or naive datetime object, according to your Django settings.

It will not attempt to convert a naive datetime to aware if timezone attribute is set to None, nor will it attempt to convert an aware timezone to a naive if the attribute is not None.

The attribute defaults to TIME_ZONE if USE_TZ is True, otherwise it is None; and can also be overridden explicitly, in a field initialisation.

note: someone should send a PR to DRF to document this behavior.

For more info see Django's time zone documentation

tutuDajuju
  • 10,307
  • 6
  • 65
  • 88
  • 1
    Hello I also have this problem where date time from DRF is not convert to timezone aware. Could you kindly explain the step to fix this? I couldn't understand what setting should I use to able to fix this. Thank you. – Norak Apr 07 '17 at 15:03
  • @Norak The [docs](https://docs.djangoproject.com/en/1.10/topics/i18n/timezones/#time-zones) say to set [`USE_TZ = True`](https://docs.djangoproject.com/en/1.10/topics/i18n/timezones/#time-zones). Make sure to also set `TIME_ZONE` to the desired value. – tutuDajuju Apr 07 '17 at 15:07
  • 2
    I set it this way and seem like it not working. Here my setting with timezone: `USE_TZ = True` `TIME_ZONE = 'Asia/Tokyo'` – Norak Apr 07 '17 at 15:14
  • At least for the current versions of Django/DRF it seems all should work well. I suggest you ask a short [MCVE](https://stackoverflow.com/help/mcve) question. – tutuDajuju Apr 07 '17 at 15:28
3

Then Django will interpret this naive datetime to aware local datetime of TIME_ZONE setting?

Django makes native datetime timezone aware at template/form level, because templates/forms are not used in Django restframework views, Django will not convert the datetimes.

In Django REST Framework, like vanilla Django, you have to set USE_TZ and TIME_ZONE to activate the timezone settings, otherwise no conversion will be done.

However until v3.8.0(released in May, 2018), Django REST Framework only convert timezone during the parsing stage(json -> model) , not in the rendering stage(model -> json). That confused a lot of people.

Leonardo.Z
  • 9,425
  • 3
  • 35
  • 38