2012-11-01 17 views
4

工作,我有一个名为user_profile像这样在一个应用程序的模型:Django的get_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 

,并在我的设置:

AUTH_PROFILE_MODULE = 'user_profile.UserProfile' 

但如果我尝试:

u = UserProfile.objects.get(pk=1) 
p = u.get_profile() 

我得到以下错误:

AttributeError: 'UserProfile' object has no attribute 'get_profile' 

我在这里错过了什么?

任何帮助将不胜感激。

回答

4

呃,你正在尝试获取UserProfile的用户配置文件。我希望你的意思是得到一个用户,然后打电话给get_profile()

+1

好的。我想通过指定'AUTH_PROFILE_MODULE ='user_profile.UserProfile''我可以访问'get_profile()'方法。另外,如果我在模板中使用'{{request.user.get_profile.username}}',我什么都没有。 –

+0

因此,如果我使用{{user.get_profile.user}},则temaplate会显示用户名。但是我不能做'{{user.get_profile.username}}'或'{{user.get_profile.email}}'。为什么是这样? –

+0

你为什么想要做这些事情?电子邮件和用户名是用户模型中的字段,这是您开始使用的!为什么你想从用户到用户配置文件,然后回到用户?哎呀! –

32

对于通过遵循旧文档获取此错误的其他人,get_profile()已在django 1.7中折旧。

相反,你可以只从如下用户访问USERPROFILE ..

u_p = user.userprofile 

其中userprofile是你UserProfile类的名称

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

+2

谢谢。这帮了我很大的时间。 – user1261774

0

我正在使用Django 1.8和.userprofile会引发错误。

为了解决这个问题,因为我在Django 1.3中设置了配置文件,在升级到1.8后,我只是将.get_profile()更改为profile,它可以工作。