python - django.db.utils.IntegrityError:“地址”列中的空值违反非空约束
问题描述
当我创建一个 Location 实例时,我收到了这个错误。
django.db.utils.IntegrityError: null value in column "address" violates not-null constraint
这是我的定位模型。
class Location(DirtyFieldsMixin, TimeStampedModel):
id = SmallUUIDField(
default=uuid_default(),
primary_key=True,
db_index=True,
editable=False,
verbose_name='ID'
)
name = models.CharField(max_length=100)
address = models.TextField(blank=True)
timezone = models.CharField(null=True, max_length=100)
phone = PhoneNumberField(blank=True)
email = models.EmailField(blank=True)
发布位置时,有效负载看起来像这样。
{'name': 'Beij the Sage', 'address': None, 'latlon': None, 'timezone': 'America/Los_Angeles', 'phone': '', 'email': ''}
我有blank=True
模型所以有None
值不address
应该抛出这个错误。
解决方案
blank=True
仅控制表单级验证。您在数据库验证层遇到错误,因为该字段默认为null=False
. 要保存空值,您必须添加null=True
到构造函数 kwargs。
address = models.TextField(blank=True, null=True)
推荐阅读
- scala - 如何在任何代码点查看所有可用的隐式及其类型?
- python - 自定义身份验证后端的问题
- javascript - 修复浏览器快速入门的谷歌日历api的锄头?
- arrays - 如何使用 C# 显示来自 json 数组的特定数据
- python - 重构处理“默认参数覆盖”的微小代码
- c++ - 尽可能快地比较 (a + sqrt(b)) 形式的两个值?
- javascript - 登录失败的客户端通知
- c - 如何以编程方式将 Linux 符号链接移动到 C 中的不同文件系统(副本)?
- macos - 更改 Safari 中的代理设置?
- wordpress - 收到来自 hostgator 的邮件说“您的帐户托管了一些恶意文件”我不知道为什么?