尝试将模型与包含 null 的 DateField 一起使用时出错

errors when trying to use model with DateField that contains null

我有一个包含 null = True、blank = True 的 DateField 模型,当我在某处使用它时,出现以下错误:

begin_date= models.DateField(verbose_name=_("Begin date"),null=True, blank=True)
errors: [{loc: ["begin_date"], msg: "none is not an allowed value", type: "type_error.none.not_allowed"}]
0: {loc: ["begin_date"], msg: "none is not an allowed value", type: "type_error.none.not_allowed"}

我使用 Pydantic 的 ValidationError,它也会因此触发

我知道有一种方法,比如将 allow_none 设置为 True,但我不知道如何正确编写它。我对编程完全陌生,英语不是我的母语,所以对错误感到抱歉。

只需删除 null=true、blank=true 并添加 begin_date=models.DateField(auto_now_add=True) 当您将日期插入数据库时​​,这将自动添加日期。 有关更多信息,请查看 django dateField 文档