繁体   English   中英

python django 中 /***/ (1048, "Column '***' cannot be null") 处的 IntegrityError

[英]IntegrityError at /***/ (1048, "Column '***' cannot be null") in python django

嗨,我收到此错误是因为我的附加用户信息模型user = models.OneToOneField(User) )中有一个字段,我没有在注册时填写(因为我想让用户稍后再填写)。

我想知道除了允许数据库中的空字段之外是否有任何方法可以解决这个问题?

干杯

尝试

user = models.OneToOneField(User, null=True, blank=True)

然后重新创建你的数据库。 您可以在https://docs.djangoproject.com/en/1.4/ref/models/fields/#null上找到更多信息

否则,您可以使用代理模型:

class UserExtraInfo(User):
#here your extra fields

在这种情况下,您不需要与 User 同时创建 UserExtraInfo 实例。 阅读更多关于https://docs.djangoproject.com/en/1.4/topics/db/models/#model-inheritance

当您将某些数据库字段定义为非空并通过时,基本上会发生完整性错误

空值或空白值

我假设您通过 django 表单将值存储在数据库中

所以在那种情况下你可以这样做

if request.method == POST: # 不管是什么方法

   get_form_obj = form.save(commit = False)

不要忘记在您的模型用户字段中进行更改,例如(null = True,blank = True)

希望这会有所帮助

对我来说,它是 serializers.py 中的 depth = 1,只需删除这部分,请求就会通过。 嵌套序列化程序因此导致了问题(在控制台中它显示 NestedSerializer(read_only=True):)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM