当我创建一个 Location 实例时,我得到了这个错误。
django.db.utils.IntegrityError: "address"列中的空值违反非空约束
这是我的定位模型。
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
,因此将 address
的值设置为 None
不应引发此错误。
最佳答案
blank=True
仅控制表单级验证。您在数据库验证层遇到错误,因为该字段默认为 null=False
。要保存空值,您必须将 null=True
添加到构造函数 kwargs。
address = models.TextField(blank=True, null=True)
关于python - django.db.utils.IntegrityError : null value in column "address" violates not-null constraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56139607/