6

I have a model in an app named user_profile like so:

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

and in my settings:

AUTH_PROFILE_MODULE = 'user_profile.UserProfile'

But if I try:

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

I get the following error:

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

What am I missing here?

Any help would be much appreciated.

3 Answers 3

38

To anyone else that gets this error by following the old documentation, get_profile() has been depreciated in django 1.7.

Instead you can just access the userprofile from the user as below..

u_p = user.userprofile

where userprofile is the name of your UserProfile class

You can also change to use the newer documentation by clicking on the desired version in the bottom right corner

0
4

Er, you're trying to get the user profile of a UserProfile. I expect you mean to get a User, then call get_profile() on that.

5
  • 1
    OK. I thought by specifying AUTH_PROFILE_MODULE = 'user_profile.UserProfile' I got access to the get_profile() method. Also if I do {{ request.user.get_profile.username }} in the template I don't get anything. Nov 1, 2012 at 18:41
  • So, if I do {{ user.get_profile.user }} the temaplate displays the username. But I cannot do {{ user.get_profile.username }} or {{ user.get_profile.email }}. Why is this? Nov 1, 2012 at 18:53
  • Why would you want to do any of those things? Email and username are fields on the user model, which is what you started with! Why would you want to go from the user to the userprofile and then back to the user? Argh! Nov 1, 2012 at 19:07
  • Because if I at some point expand the UserProfile, I may want to get those specific fields and the generic fields such as email. I though all of those were reachable via get_profile(). Nov 1, 2012 at 19:14
  • How should I then get email variable in the templates? Ok I guess that {{ user.email }} works. Nov 1, 2012 at 19:14
0

I am using Django 1.8 and .userprofile throws an error.

To get around this since I had profiles setup in Django 1.3, after upgrading to 1.8 I just changed .get_profile() to profile and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.