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

10,163

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

Share:
10,163
jayt
Author by

jayt

Updated on June 27, 2022

Comments

  • jayt
    jayt almost 2 years

    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

  • jayt
    jayt over 7 years
    Awesome, that works. Do you know how I'd write that with the '+' that is suggested by Django here: docs.djangoproject.com/en/1.10/ref/models/fields/… to remove backwards relationship? I've tried stuff like related_name='profile''+', and similar. I've found nothing on SO about this, and the Django docs example only uses '+' by itself.