Django:时区问题 [英] Django: time zone issue

查看:167
本文介绍了Django:时区问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我们的数据库服务器(RH)有TIME_ZONE =Europe / London指定。而在Django settings.py中,我们指定了TIME_ZONE =America / New_York。



而且,在我的Model类中,我已经指定了:

  created = models.DateTimeField(editable = False,auto_now = False,auto_now_add = True)
modified = models.DateTimeField(editable = False, auto_now = True,auto_now_add = True)

当我看看管理员网站中的数据时,我得到UTC / GMT时间,而不是东部。



我以为Django自动将Django的时区指定为American / New_York,所有时间都是自动调整的。



感谢
Eric

解决方案

依赖于日期/时间'automagic'是危险的,这些auto_add模型参数是一个陷阱。始终了解您正在处理的时区。 Python通过将tzinfo成员附加到其datetime对象来使这更容易。默认情况下,这些对象是天真的,我鼓励您始终附加tzinfo细节。仍然Python需要一些额外的帮助与 python-dateutil pytz (我使用的)。这是一个普遍的规则,尽管如此 - 总是将您的数据时间存储在数据库中,如UTC。



为什么?您的用户可能在不同的本地人,手机和笔记本电脑旅行,服务器配置错误或镜像在不同的时区。这么多头疼。数据不应该是天真的,如果它们(如数据库中),并且您需要上下文,还可以在表中包含时区字段。



所以在你的情况下。


  1. 不要使用auto_now字段,而是使用自定义save()。

  2. UTC数据库中的UTC

  3. 如果您需要知道时区 - 说用户事件 - 也可以将时区存储在数据库中。

  4. 转换到必要/请求的时区

如果您使用的是pytz, localize()方法很棒。 Python的datetime对象具有有用的replace()和astimezone()。



还有一个注意事项,如果你的数据库是时区天真(像MySQL),请确保你的数据时间是UTC然后使用replace(tzinfo = None),因为数据库连接器无法处理tz感知对象。



这是一个线程,详细介绍了Django的auto_now字段。


NOTE: I deleted the question as it existed previously and providing only the relevant info here.

Our database server (RH) has TIME_ZONE = "Europe/London" specified. And, within the Django settings.py, we specify TIME_ZONE = "America/New_York".

And, in my Model class I have specified:

created = models.DateTimeField(editable=False,auto_now=False, auto_now_add=True)
modified = models.DateTimeField(editable=False,auto_now=True, auto_now_add=True)

When I then go look at the data in the admin site, I get UTC/GMT time instead of Eastern.

I thought that all time is adjusted automagically by Django since I specified "America/New_York" as Django's Time Zone.

Any help/clarification is appreciated.

Thanks Eric

解决方案

Relying on date/time 'automagic' is dangerous and these auto_add model parameters are a trap. Always understand the timezone(s) you are dealing with. Python makes this easier by attaching a tzinfo member to its datetime objects. While these objects are 'naive' by default, I encourage you to always attach tzinfo detail. Still Python needs some extra help with either python-dateutil or pytz (what I use). Here's a universal rule though - always store your datetimes in a database as UTC.

Why? Your users may be in different locals, mobile phones and laptops travel, servers are misconfigured or mirrored in different timezones. So many headaches. Datetimes should never be naive and if they are (as in a database) and you need the context, also include a timezone field in the table.

So in your case.

  1. Don't use the auto_now fields, use a custom save() instead.
  2. Store UTC in the database
  3. If you need to know the timezone - for say a user event - store the timezone in the database as well.
  4. Convert to the necessary/requested timezone

If you are using pytz, the localize() method is great. Python's datetime object has the useful replace() and astimezone().

One more note, if your database is timezone naive (like MySQL) make sure your datetimes are in UTC and then use replace(tzinfo=None) because the database connector can't handle tz-aware objects.

Here is a thread with detail on Django's auto_now fields.

这篇关于Django:时区问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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