Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 674
jayt Avatar asked Sep 17 '16 16:09

jayt


1 Answers

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

like image 67
Daniel Roseman Avatar answered Nov 15 '22 05:11

Daniel Roseman