默认值为DateTimeField,用于在具有激活的时区支持的Django项目中进行南迁移 [英] Default value of DateTimeField for South migration in Django project with activated timezone support
问题描述
我在Django 1.4.3项目中为South 0.7.6创建了一个模式迁移,支持启用时区支持。
模式迁移包括添加<$一个表上的c $ c> DateTimeField (含 auto_now = True
)
在创建迁移时,南方提示:
字段MyTable.my_field没有指定默认值,但不是空值。
由于您添加此字段,您必须指定一个默认
值用于现有行。您要:
1.立即退出,并在models.py
中的字段中添加默认值2.现在指定一个现有的值用于
如果我不在乎现有行的这个值,那么给出的正确的一次性值是多少(我只想要迁移成功而没有警告)?
到目前为止,我使用了 datetime.datetime.utcnow()
。但是,在应用迁移时,我得到以下信息:
C:\Python27\lib\site- packages \\ django\db\models\fields\__init __。py:808:
RuntimeWarning:DateTimeField收到一个天真的日期时间(2013-01-16 00:00:00)
,而时区支持活跃。南方似乎没有导入pytz或Django助手类,所以我怎么能给一个时区 - 感觉默认值在这里?解决方案手动编辑南方创建并添加的迁移文件:
from django.utils import timezone
然后在迁移文件中找到要添加的字段,并将其默认值
设置为 timezone.now()
。 / p>
I'm creating a schema migration with South 0.7.6 for my Django 1.4.3 project with enabled timezone support.
The schema migration includes adding a DateTimeField
(with auto_now=True
) on one table.
When creating the migration, South prompts me:
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
What's the correct one-off value to give here, if I don't care about this value for existing rows (I just want the migration to succeed without warnings)?
So far, I used datetime.datetime.utcnow()
. However, then I get the following when applying the migration:
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 does not seem to import pytz or the Django helper classes, so how can I give a timezone-aware default value here?
解决方案 Manually edit the migration file that South created and add:
from django.utils import timezone
Then find the field that you are adding in the migration file and set its default
to timezone.now()
.
这篇关于默认值为DateTimeField,用于在具有激活的时区支持的Django项目中进行南迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!