具有激活时区支持的Django项目中用于南迁移的DateTimeField的默认值

Hen*_*ger 7 python django django-south django-timezone

我正在为我的Django 1.4.3项目创建一个使用South 0.7.6的模式迁移,并启用了时区支持.

模式迁移包括在一个表上添加DateTimeField(with auto_now=True).

在创建迁移时,South提示我:

The field 'MyTable.my_field' does not have a default specified, yet is NOT NULL.
Since you are adding this field, you MUST specify a default
value to use for existing rows. Would you like to:
 1. Quit now, and add a default to the field in models.py
 2. Specify a one-off value to use for existing columns now
Run Code Online (Sandbox Code Playgroud)

如果我不关心现有行的这个值,那么这里给出的正确的一次性值是多少(我只希望迁移成功而没有警告)?

到目前为止,我用过datetime.datetime.utcnow().但是,在应用迁移时,我得到以下内容:

C:\Python27\lib\site-packages\django\db\models\fields\__init__.py:808:
RuntimeWarning: DateTimeField received a naive datetime (2013-01-16 00:00:00)
while time zone support is active.
Run Code Online (Sandbox Code Playgroud)

South似乎没有导入pytz或Django帮助程序类,那么如何在此处提供时区感知的默认值?

dge*_*gel 6

手动编辑South创建的迁移文件并添加:

from django.utils import timezone
Run Code Online (Sandbox Code Playgroud)

然后在迁移文件中找到要添加的字段并将其设置defaulttimezone.now().

  • 谢谢,这似乎工作!每次我使用`DateTimeField`时,不必手动编辑我的南迁移文件.这是目前的最佳做法吗? (3认同)