python - 尝试将模型与包含 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,但我不知道如何正确编写它。我对编程完全陌生,英语不是我的母语,所以对错误感到抱歉。
解决方案
只需删除 null=true、blank=true 并添加 begin_date=models.DateField(auto_now_add=True) 这将在您将日期插入数据库时自动添加。 更多检查 django dateField 文档
推荐阅读
- python - Tensorboard 什么都不显示
- nonlinear-optimization - 在 OpenMDAO 中,有没有办法确保在继续计算之前遵守约束?
- kubernetes - AWS EBS 如何获取文件更改通知
- r - 循环遍历函数并将输出堆叠到 R 中的数据集中
- git - 您不能推送“serena@ai.com”的提交。您只能推送使用您自己的已验证电子邮件之一提交的提交
- python - 如何将条形图和折线图一起绘制并用日期格式化 x 轴
- python - 当输入字符串与键字符串相同时,做一些事情
- pine-script - 将研究脚本转换为策略时,Pinescript 中出现未声明的标识符错误
- python - 使用 Python SDK 在 GCP 中获取区域集群
- python - 耦合两个不同的非继承类参数/变量的最佳方式