DateTimeField设置为默认字段



以下模型的日期字段需要一个默认值:

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=真";但它也不起作用。我做错了什么?

auto_now_true设置字段以在创建时更新的唯一方法,因为文档中指出

选项auto_now_addauto_nowdefault是互斥的。这些选项的任何组合都将导致错误。

如果auto_now_true不起作用,则需要引发一个错误。

最新更新