Custom Search

Wednesday, November 30, 2016

python django AttributeError: 'User' object has no attribute 'get_profile'

Error:
request.user.get_profile()
AttributeError: 'User' object has no attribute 'get_profile'

Fix:
a)
Remove 
AUTH_PROFILE_MODULE = "core.userprofile"
from settings.py

b)
Change "ForeignKey" to "OneToOneField"
Then add keyword arg related_name='profile'
class UserProfile(models.Model):  
    user = models.ForeignKey(User, unique=True)
To
class UserProfile(models.Model):  
    user = models.OneToOneField(User, unique=True, related_name='profile')

c)
Change
request.user.get_profile()
To
request.user.profile


No comments:

Post a Comment