gpt4 book ai didi

python - DateTimeField 设置为默认字段

转载 作者:行者123 更新时间:2023-11-29 12:19:29 26 4
gpt4 key购买 nike

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

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, and default 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com