Django get_profile()方法不适用于扩展用户模型

Dar*_*ech 4 django django-models

我在一个名为的应用程序中有一个模型user_profile:

from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
    user = models.OneToOneField(User)

    def __unicode__(self):
        return self.user.username
Run Code Online (Sandbox Code Playgroud)

在我的设置中:

AUTH_PROFILE_MODULE = 'user_profile.UserProfile'
Run Code Online (Sandbox Code Playgroud)

但如果我尝试:

u = UserProfile.objects.get(pk=1)
p = u.get_profile()
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

AttributeError: 'UserProfile' object has no attribute 'get_profile'
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?

任何帮助将非常感激.

Say*_*yse 33

对于因遵循旧文档而出现此错误的任何其他人,get_profile()已在django 1.7中弃用.

相反,您只需从用户访问userprofile,如下所示.

u_p = user.userprofile
Run Code Online (Sandbox Code Playgroud)

userprofileUserProfile班级的名字在哪里

您还可以通过单击右下角的所需版本来更改以使用较新的文档

  • 谢谢.这帮我度过了难关. (2认同)

Dan*_*man 4

呃,您正在尝试获取 UserProfile 的用户配置文件。我希望您的意思是找到一个用户,然后调用get_profile()它。