首页 > 解决方案 > 尝试将模型与包含 null 的 DateField 一起使用时出错

问题描述

我有一个包含 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,但我不知道如何正确编写它。我对编程完全陌生,英语不是我的母语,所以对错误感到抱歉。

标签: pythondjangopydantic

解决方案


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


推荐阅读