Django设置USE_TZ,TIME_ZONE和Django Rest框架 [英] Django settings USE_TZ, TIME_ZONE and django rest framework

查看:243
本文介绍了Django设置USE_TZ,TIME_ZONE和Django Rest框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django教程中,有一个句子描述如下.

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

TIME_ZONE

TIME_ZONE

...

当USE_TZ为False时,这是Django将存储的时区所有日期时间.当USE_TZ为True时,这是默认的时区Django将用于在模板中显示日期时间并进行解释在表格中输入的日期时间.

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.

...

当django rest框架从请求中获取朴素的datetime数据时.然后Django会将此原始日期时间解释为知道TIME_ZONE设置的本地日期时间吗?如果是正确的,它如何运作?

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?

提前谢谢!

推荐答案

通常,在解析

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'].

这基本上意味着输入可以使用 ISO-8601 格式和该字段通常可以使用确定根据您的Django设置创建感知的还是天真的 datetime 对象.

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.

如果 timezone 属性设置为 None ,它将不会尝试将朴素的 datetime 转换为可识别的,如果属性不是 None ,它也不会尝试将感知的时区转换为幼稚的.

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.

该属性默认为 TIME_ZONE 如果 USE_TZ 为True,否则为 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.

注意: 有人应向PR发送一个PR来记录此行为.

有关更多信息,请参见 Django的时区文档

For more info see Django's time zone documentation

这篇关于Django设置USE_TZ,TIME_ZONE和Django Rest框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆