RelatedObjectDoesNotExist用户没有个人资料

时间:2017-05-09 19:01:29

标签: python django

我是Django的新手。我正在使用内置的Django User模型和Usercreation表单创建用户注册。我正在尝试扩展Django中的内置用户模型,所以我可以包含一个额外的字段公司名称。我遇到了下面的这个反复出现的错误。我真的很感激任何帮助,试图解决这个问题。

RelatedObjectDoesNotExist用户没有个人资料

(1)这是我的Profile模型的Model.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    Company_name = models.CharField(max_length=30)


@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)


@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

(2.)这是我的views.py.I我试图在每次创建新用户实例时更新配置文件模型。我似乎必须将create_user_profile和save_user_profile函数链接到我的视图但我不是太确定如何做到这一点。

def Register(request):
    if request.method == 'POST':
        User_form = RegisterRecruiterForm(request.POST, instance=request.user)
        Profile_form = ProfileForm(request.POST, instance=request.user.profile)
        if User_form.is_valid() and Profile_form. is_valid():
            User_form.save()
            Profile_form.save()
            return HttpResponse('you are now registered')
    else:
        User_form = RegisterRecruiterForm(instance=request.user)
        Profile_form = ProfileForm(instance=request.user.profile)
    return render(request, 'Register.html', {
        'User_form': User_form,
        'Profile_form': Profile_form

        })

3.这是我的forms.py。

    class RegisterRecruiterForm(UserCreationForm):

        email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')


    class Meta:
        model = User
        fields = ('username', 'email', 'password1', 'password2')


    def save(self, commit=True):
        user = super(RegisterRecruiterForm, self).save(commit=False)
        user.email(self.cleaned_data["email"])
        user.username(self.cleaned_data["username "])
        user.password(self.cleaned_data["password1 "])
        user.password(self.cleaned_data["password2 "])
        if user.password1 != user.password2:
            raise forms.validationError("Password do not match")
        return Profile.Company_name

        if commit:

                user.save()
        return user


    class ProfileForm(forms.ModelForm):
        Company_name = forms.CharField(max_length=10,help_text='Required')

        class Meta:
            model = Profile
            fields = ('Company_name',)

def save(self, commit=True):
    Profile = super(ProfileForm, self).save(commit=False)
    Profile.Company_name(self.cleaned_data["Company_name"])

    if commit:

            Profile.save()
    return Profile

1 个答案:

答案 0 :(得分:1)

正如我所看到的,你的问题是你正试图获得" request.user.profile"在这一行:

Profile_form = ProfileForm(request.POST, instance=request.user.profile)

在你的模特中,"用户"没有"个人资料"但是"简介"有一个"用户",所以获取此个人资料的正确方法是:

profile = Profile.objects.get(user = request.user)
Profile_form = ProfileForm(request.POST, instance = profile)

这解决了" RelatedObjectDoesNotExist"错误。如果您有其他错误,可以打开一个新问题。

最后,你"注册"功能将是:

def Register(request):
    if request.method == 'POST':
        User_form = RegisterRecruiterForm(request.POST, instance=request.user)
        profile = Profile.objects.get(user = request.user)
        Profile_form = ProfileForm(request.POST, instance = profile)
        if User_form.is_valid() and Profile_form. is_valid():
            User_form.save()
            Profile_form.save()
            return HttpResponse('you are now registered')
    else:
        User_form = RegisterRecruiterForm(instance=request.user)
        profile = Profile.objects.get(user = request.user)
        Profile_form = ProfileForm(request.POST, instance = profile)
    return render(request, 'Register.html', {
        'User_form': User_form,
        'Profile_form': Profile_form

        })