当尝试使用包含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,它也会触发,因为

我知道有一种方法,比如set allow_none为True,但我不知道如何正确地编写它。我对编程完全陌生,英语也不是我的母语,所以很抱歉我犯了错误。

删除null=true, blank=true并添加begin_date=models.DateField(auto_now_add= true)这将自动添加日期,当你插入到数据库。查看django dateField文档

最新更新