【发布时间】:2015-11-24 23:50:01
【问题描述】:
Django 中的以下模型(Python 3.4、Django 1.7.4 w/PostgreSQL)需要其 DateTimeField 的默认值:
from django.utils import timezone as django_tz
class AvailabilityRuleOnce(AvailabilityRule):
class Meta:
app_label = 'configuration'
objects = AvailabilityRuleOnceManager()
starting_time = django_models.DateTimeField(
'Starting time for the rule', default=django_tz.now, blank=True
)
ending_time = django_models.DateTimeField(
'Ending time for the rule', default=django_tz.now, blank=True
)
尝试应用迁移时,抛出以下异常:
django.db.utils.ProgrammingError: column "ending_time" cannot be cast automatically to type timestamp with time zone
HINT: You might need to specify "USING ending_time::timestamp with time zone".
在 Django 文档中,他们推荐了此选项,我还尝试了其他可以在线找到的选项,例如添加“auto_now_add=True”,但它也不起作用。我做错了什么?
【问题讨论】:
-
真正的问题是 Django 的迁移不会擦除带有数据字段的列,这在更新默认值之前是必需的。
标签: python django postgresql django-models