Django-rest-framework timezone aware renderers/parsers

后端 未结 3 1617
情书的邮戳
情书的邮戳 2020-12-25 14:49

I\'m building an app that would serve people located in different places arround the world.
I\'m using Django-Rest-Framwork for the communication between the clients and

3条回答
  •  时光说笑
    2020-12-25 14:51

    Since Django REST Framework v3.8.0(released in May, 2018), you don't need a custom DateTimeField any more.

    In previous versions, Django REST Framework only convert native datetime to timezone aware datetime when parsing the date(DateTimeField.to_internal_value()), but do not convert when rendering the datatime field(DateTimeField.to_representation()). This is fixed in DRF v3.8.0.

    You may need to change the following settings:

    1. USE_TZ must be True
    2. set TIME_ZONE to specify a default timezone
    3. set REST_FRAMEWORK.DATETIME_FORMAT to the format that fits your frontend code.

提交回复
热议问题