RelatedObjectDoesNotExist:用户没有用户个人资料 [英] RelatedObjectDoesNotExist: User has no userprofile

查看:178
本文介绍了RelatedObjectDoesNotExist:用户没有用户个人资料的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经用字段 score 扩展了我的用户,像这样:

So I've extended my user with the field score like this:

models.py:

models.py:

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    score = models.IntegerField(default=0);

这似乎与推荐方式

然后我尝试在我的视图中访问用户的用户个人资料

Then I tried to access the user's userprofile in my views:

views.py:

player = request.user.userprofile

这似乎也与建议的方式保持一致,但这就是我得到错误的地方:

which seemed to align with the recommended way as well, but that is where I get my error:

RelatedObjectDoesNotExist
User has no userprofile.

如果我将 userprofile 更改为其他内容出现另一个错误:

If I change userprofile to something else I get another error:

AttributeError
'User' object has no attribute 'flowerpot'

当我尝试以下代码时:

print request.user
print UserProfile.objects.all()

我明白了控制台输出:

post_test1
[]

编辑

我有两个超级用户,在扩展用户之前创建了七个用户,还有一个用户(post_test1)我是在扩展用户之后创建的。

EDIT
I have two superusers, seven users I created before extending the user, and one user (post_test1) that I created after extending the user.

编辑2

显而易见,我需要创建一个 post_save 处理程序,该处理程序将在创建User对象时创建一个新的配置文件。

It seams clear that what I need is to create a post_save handler that creates a new profile whenever the User object is created.

当我阅读它时,这似乎很简单,转到页面被链接到的,这是Django发送的所有信号的列表。我抬起 post_save 并说:

This seemed simple enough when I read it, went to the page that was linked to, which was a list of all the signals Django sends. I looked up post_save and it said:


像pre_save,但在save()方法的结尾。

Like pre_save, but sent at the end of the save() method.

好的,所以我查找 pre_save 并显示:

Alright, so I look up pre_save and it says:


这是在模型的save()方法开始时发送的。

This is sent at the beginning of a model’s save() method.

我这样解释:当我创建用户(在我的 views.py 中)时,<$应该调用c $ c> save()方法,直到现在还不是这种情况,然后应调用 post_save send(?),它将在创建User对象时创建一个新的配置文件!

I've interpreted it like this: When I create my user (in my views.py) the save() method should be called, which hasn't been the case up until now, and after that a post_save should be sent(?), which will create a new profile whenever the User object is created!

所以现在我准备开始查看示例了,所以我在Google上搜索:

So now I'm ready to start looking at examples, so I google:

django邮件保存示例

在这里似乎应该添加类似装饰器的东西 @receiver(post_save,... < br>
在这里似乎应该更改多个文件并编写信号定义?

似乎还暗示了多个文件(包括 signals.py

django post save example
Here it looks like I'm supposed to add something that looks like a decorator @receiver(post_save, ...
Here it looks like I'm supposed to alter multiple files and write a signal definition?
This one also seems to imply multiple files (including a signals.py)

似乎比我最初想象的要多得多。

It looks like there's a lot more to it than I first thought. Could anyone here either explain how I'm to do this or show me to some good resources on how signals work?

现在我的 create_user 视图如下:

def create_user(request):
    if request.method == "POST":
        form = UserCreationForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data["username"]
            password = form.cleaned_data["password1"]
            new_user = User.objects.create_user(username=username, password=password)
            return redirect('play')
    else:
        form = UserCreationForm()

    return render(request, 'antonymapp/create_user.html', {'form': form})

返回前我应该打电话给 new_user.save()吗?如果是,为什么直到现在仍能解决?我在测试此视图时创建了很多用户。似乎也应该在此处添加 post_save.connect(create_profile,sender = User)

Should I call new_user.save() before returning? If yes, why has it worked up until now? I have a bunch of users that I've created while testing this view. It also looks like somewhere around here post_save.connect(create_profile, sender=User) should be added?

推荐答案

首先必须为用户创建用户配置文件

profile = UserProfile.objects.create(user=request.user)

在您的views.py中,您可以使用 get_or_create ,以便在没有用户配置文件的情况下为该用户创建用户配置文件。

In your views.py you can use get_or_create so that a userprofile is created for a user if the user doesn't have one.

player, created = UserProfile.objects.get_or_create(user=request.user)

更新:要在每次创建新用户时自动创建用户个人资料,请使用单个字符。在 myapp / signals.py 中执行以下操作:

UPDATE: For automatically creating user profiles every time a new user is made, use singals. In myapp/signals.py do something like this:

@receiver(post_save, sender=User, dispatch_uid='save_new_user_profile')
def save_profile(sender, instance, created, **kwargs):
    user = instance
    if created:
        profile = UserProfile(user=user)
        profile.save()

这篇关于RelatedObjectDoesNotExist:用户没有用户个人资料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆