Django 'User' object has no attribute 'profile'

Can someone help me? I've been going around and around with questions here on SO but can't get the answer.

I'm getting 'User' object has no attribute 'profile'

Here's my user model

class Profile(models.Model):

    NARRATESTATUS = (
        ('PAS', 'Passed'),
        ('REV', 'For_Review'),
        ('ACC', 'Narration_Accepted'),
    )

    TRANSLATESTATUS = (
        ('PSD', 'Passed'),
        ('RVW', 'For_Review'),
        ('ACP', 'Translation_Accepted'),
    )

    user = models.ManyToManyField(User, max_length=50, related_name='userprofile')
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')
    narrate = models.TextField(max_length=9999, default='Enter Narration')
    review_narration = models.CharField(max_length=50, choices=NARRATESTATUS, default='Reviewing Narration')
    translate = models.TextField(max_length=9999, default='Enter Translation')
    review_translation = models.CharField(max_length=50, choices=TRANSLATESTATUS, default='Reviewing Translation')

    def __str__(self):
        return self.user

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

        img = Image.open(self.image.path)

        if img.height > 300 or img.width > 300:
           output_size = (300, 300)
           img.thumbnail(output_size)
           img.save(self.image.path)

I dont know why it's throwing 'User' object has no attribute 'profile'

Someone familiar with this?

Upvotes: 1

Views: 8649

Answers (3)

Dereje Hinsermu
Dereje Hinsermu

Reputation: 1

Try to change user to User:

User = models.ManyToManyField(User, max_length=50, related_name='userprofile')

Upvotes: 0

Tinashe Mphisa
Tinashe Mphisa

Reputation: 35

Make sure your app is available in settings.py INSTALLED_APPS

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599450

Your model doesn't make any sense. You have a many to many relationship between Profile and User. As the name implies, that means each user can have multiple profiles, and each profile can be associated with multiple users.

Surely that isn't what you want: you need a single user to be associated with a single profile, and that profile to belong to that user only. So you should use a one to one field.

user = models.OneToOneField(User, related_name='profile')

Note two other changes: max_length is for strings, not relationships; and more to the point, since you want to access the Profile via user.profile you have to set the related_name to profile.

Upvotes: 5

Related Questions