Django:IntegrityError列'user_id'不能为空

时间:2012-12-18 16:32:31

标签: django django-models

在注册时保存我的用户后,我收到以下错误消息。有人能解释一下原因吗?

由于

错误消息

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()

models.py

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

    def __unicode__(self):
        return self.name

1 个答案:

答案 0 :(得分:6)

错误是您保存成员而不是用户。创建成员时,您没有分配用户。