Django的DateTimeField与UTC偏移量? [英] django DateTimeField with UTC offset?

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

问题描述

我有一个带有DateTimeField的模型:

I have a model with a DateTimeField:

deadline = models.DateTimeField(verbose_name="Valid unitl", null=True, blank=True)

应该允许用户在字段中输入日期,时间和时区信息。这是我想要的格式:

Users should be allowed to input date, time and timezone info in the field. This is my desired format:

2012-12-31 23:30 +0430

我希望时间在存储到db之前会转换为UTC。因此,我尝试为此使用模型表单,但是如果输入上面的值,它将引发输入有效的日期/时间。对该DateTimeField进行验证错误。

I expect the time will get converted to UTC before storing to db. So I tried using a model form for that, but it throws Enter a valid date/time. validation error on that DateTimeField if I enter the value above.

这是在settings.py中:

This is in settings.py:

DATE_INPUT_FORMATS = ('%Y-%m-%d %H:%M %Z', )

我想念什么?

编辑:

根据ВидулПетров的建议,尝试使用表单字段:

As per Видул Петров's suggestion, tried to use a form field:

deadline2 = forms.DateTimeField(input_formats=['%Y-%m-%d %H:%M %Z',],

具有相同的效果:输入有效的日期/时间。

编辑2

看来日期时间可以无法处理%z参数,这会引发ValueError:

It appears that datetime can't handle the "%z" parameter. This throws a ValueError:

datetime.datetime.strptime(value, format)

所以我在控制台中对其进行了测试:

So I tested it in console:

>>> import datetime
>>> datetime.datetime.strptime('2012-12-30 19:00 +0100', "%Y-%m-%d %H:%M %z")
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 317, in _strptime
(bad_directive, format))
ValueError: 'z' is a bad directive in format '%Y-%m-%d %H:%M %z'

还尝试了pytz:

>>> import pytz
>>> pytz.datetime.datetime.strptime('2012-12-30 19:00 +0100', "%Y-%m-%d %H:%M %z")
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 317, in _strptime
(bad_directive, format))
ValueError: 'z' is a bad directive in format '%Y-%m-%d %H:%M %z'

我真的认为这应该可行。我是否错过了某些文档中的其他说明?

I really feel this should work. Did I miss some part of the docs that says otherwise?

推荐答案

设置 USE_TZ = True 在您的设置中,Django将日期和时间信息存储在数据库中的UTC中,否则它将存储天真日期时间(不带时区的日期时间)。

When you set USE_TZ = True in your settings, Django stores date and time information in UTC in the database otherwise it will store naive date time (date time without timezone).

在大多数情况下,使用Django的时区支持非常方便,因为输入和输出日期时间将由Django自动转换。

In most cases using Django's time zones support is very convenient because input and output datetime will be automatically translate by Django.

但是如果您确实需要从用户输入的时区,您需要设置 USE_TZ = False ,然后使用DateTimeField(它是朴素的日期时间)和CharField一起将时区信息存储在model.py中。

But if you really need timezone input from your user, you will need to set USE_TZ = False then use DateTimeField which is naive datetime along with CharField to store timezone information in your models.py.

参考: https:// docs。 djangoproject.com/en/1.4/topics/i18n/timezones/

这篇关于Django的DateTimeField与UTC偏移量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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