gpt4 book ai didi

Django:IntegrityError 列 'user_id' 不能为空

转载 作者:行者123 更新时间:2023-12-01 01:24:41 27 4
gpt4 key购买 nike

在注册时保存我的用户后,我收到以下错误消息。有人可以解释为什么吗?

谢谢

错误信息

IntegrityError at /member/registration/
(1048, "Column 'user_id' cannot be null")
Exception Type: IntegrityError at /member/registration/
Exception Value: (1048, "Column 'user_id' cannot be null")

view.py
 if form.is_valid():
# If form has passed all validation checks then continue to save member.
user = User.objects.create_user(
username=form.cleaned_data['username'],
email=form.cleaned_data['email'],
password=form.cleaned_data['password']
)
user.save()
#member = User.get_profile()
#member.name = form.cleaned_data['name']
#member.save()
member = Member(
name=form.cleaned_data['name']
)
member.save()

模型.py
class Member (models.Model):
user = models.OneToOneField(User)
name = models.CharField(max_length=100)

def __unicode__(self):
return self.name

最佳答案

错误是当您保存成员而不是用户时。创建成员时,您并未分配用户。

关于Django:IntegrityError 列 'user_id' 不能为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13937395/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com