jayt
jayt

Reputation: 768

Django 1.9 error - 'User' object has no attribute 'profile'

So I recently added an optional user profile model that is linked to a user via a OneToOneField like so:

class UserProfile(models.Model): # Creating class
    user = models.OneToOneField(User, on_delete=models.CASCADE)

This worked fine, and my current UserProfile models were intact both before and after I added in this field to link the profile to a user.

The problem comes when I log in on the website as a valid user, after submitting the log in form comes an error:

AttributeError at /login/

'User' object has no attribute 'profile'

I have searched through my files for anything along the lines of 'User.profile', and similar queries, however I can't seem to find anything that would allow me to properly log in as a user.

I wasn't sure what other code to post here, and didn't want to spam this question with potentially unnecessary code so tell me if there's anything more specific (views.py, models.py, forms.py, etc) that will be needed to solve this sort of problem, or if you know of any possible solutions.

Thank you

Upvotes: 8

Views: 10982

Answers (2)

Tinashe Mphisa
Tinashe Mphisa

Reputation: 35

Make sure your app is in settins.py INSTALLED_APPS

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599450

You haven't set any related_name attribute on that one-to-one field, so the reverse accessor will be called userprofile not profile.

Upvotes: 14

Related Questions