Django 设置 USE_TZ、TIME_ZONE 和 django rest 框架

标签 django datetime django-rest-framework django-timezone

在 Django 教程中,有一句话描述如下。

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.

...



当 django rest 框架从请求中获取原始日期时间数据时。那么 Django 会将此天真的日期时间解释为了解 TIME_ZONE 设置的本地日期时间吗?如果它是正确的,它是如何工作的?

提前致谢!

最佳答案

通常,在解析 serializer's DateTimeField 中的请求时,在 DRF 中确定输入时区。 (类似于表单字段)。

您可以控制此类输入的格式,甚至还有一个通用设置 DATETIME_INPUT_FORMATS 默认为 ['iso-8601'] .

这基本上意味着输入既可以省略也可以使用 ISO-8601 指定时区。格式和 the field will generally be able to determine whether to create an aware or naive datetime object , 根据您的 Django 设置。

它不会尝试转换一个幼稚的 datetime要知道 如果 timezone属性设置为 None ,也不会尝试将感知时区转换为幼稚时区 如果属性不是 None .

该属性默认为 TIME_ZONE 如果 USE_TZ 为真,否则为 None ;并且也可以在字段初始化中显式覆盖。

注:有人应该向 DRF 发送 PR 来记录这种行为。

欲了解更多信息,请参阅 Django's time zone documentation

关于Django 设置 USE_TZ、TIME_ZONE 和 django rest 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38365067/

相关文章:

python - 如何在 Django 中查询泛型关系

python - Django celery worker 向前端发送实时状态和结果消息

django - DRF - 如何让 WritableField 不将整个数据库加载到内存中?

python - Stripe 支付 APIConnection 错误

java - 如何在 Joda-Time 中总结岁月

python - 当时间序列中的下一个值较大时过滤数据

django - Django REST框架发布对象数组

Django:AttributeError:类型对象 'MyAppConfig'没有属性 'rpartition'

oracle - ORA-01830 : date format picture ends before converting entire input string in TOAD

django - 如何应用节流只是为了在 DRF View 集中创建 Action ?