Django DateTimeField Stores datetime regardless of the tzinfo

后端 未结 1 561
一向
一向 2020-12-20 04:55

Why django DateTimeFieldrestore tzinfo in datetime to ?

Below is my test code.

Is it normal or

相关标签:
1条回答
  • 2020-12-20 05:11

    Make sure you read Django's timezone documentation. The approach is succinctly stated in the very first sentence:

    When support for time zones is enabled, Django stores datetime information in UTC in the database, uses time-zone-aware datetime objects internally, and translates them to the end user’s time zone in templates and forms.

    So, yes, it is normal that you see the return value from the database in UTC.

    As for why, the documentation states:

    Even if your Web site is available in only one time zone, it’s still good practice to store data in UTC in your database. The main reason is Daylight Saving Time (DST). Many countries have a system of DST, where clocks are moved forward in spring and backward in autumn. If you’re working in local time, you’re likely to encounter errors twice a year, when the transitions happen.

    It also links to a more detailed description in the pytz documentation.

    0 讨论(0)
提交回复
热议问题