Django get_profile () method does not work with extended user model

I have a model in an application called user_profile , for example:

 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 greatly appreciated.

+4
source share
4 answers

Er, you are trying to get the user profile in UserProfile. I expect you want to get the user, and then call get_profile() .

+4
source

To someone who gets this error following the old documentation, get_profile() was discounted in django 1.7.

Instead, you can simply access the user profile from the user, as shown below.

 u_p = user.userprofile 

where userprofile is the name of your userprofile class

You can also change the use of the new documentation by clicking on the version in the lower right corner.

+33
source

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

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

0
source

You can follow the link below, it helped me. The above userprofile or get_profile is not allowed. related_name

0
source

Source: https://habr.com/ru/post/1443506/


All Articles