我正在使用 South 0.7.6 为启用时区支持的 Django 1.4.3 项目创建模式迁移。
架构迁移包括在一个表上添加一个DateTimeField
(使用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
如果我不关心现有行的这个值(我只希望迁移在没有警告的情况下成功),那么在这里给出的正确一次性值是多少?
到目前为止,我使用了 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.
South 似乎没有导入 pytz 或 Django 帮助程序类,那么我如何在此处提供时区感知默认值?
最佳答案
手动编辑 South 创建的迁移文件并添加:
from django.utils import timezone
然后在迁移文件中找到您要添加的字段并将其默认
设置为timezone.now()
。
关于python - 启用时区支持的 Django 项目中南方迁移的 DateTimeField 默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14362414/