Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django get_profile() method not working on extended User model

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.

like image 491
Darwin Tech Avatar asked Nov 01 '12 18:11

Darwin Tech


2 Answers

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

like image 105
Sayse Avatar answered Sep 19 '22 01:09

Sayse


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.

like image 41
Daniel Roseman Avatar answered Sep 20 '22 01:09

Daniel Roseman