以下模型的日期字段需要一个默认值:
from django.utils import timezone as django_tz
import psycopg2
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”,但它也不起作用。我做错了什么?
最佳答案
auto_now_true
是唯一将字段设置为在创建时更新的方法,as the documentation states
The options
auto_now_add
,auto_now
, anddefault
are mutually exclusive. Any combination of these options will result in an error.
如果 auto_now_true
不工作,您需要提出错误。
关于python - DateTimeField 设置为默认字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33906078/