Re: AttributeError: object has no attribute 'user' while trying to access instance.user.id

150 views
Skip to first unread message
Message has been deleted

coolguy

unread,
Sep 12, 2020, 2:20:11 PM9/12/20
to Django users
Please share the complete employee model. It seems you are missing User foreign key in it.

On Saturday, September 12, 2020 at 2:03:20 PM UTC-4 mislav....@gmail.com wrote:
Hey guys,

I have the following code in models.py file in one of my apps:

def get_upload_path(instance, filename):
    extension = filename.split('.')[-1]
    return "employees/media/users/{0}/profile_picture.{1}".format(instance.user.id, extension)

class Employee(models.Model):
    # some attributes here
    profile_picture = models.ImageField(upload_to=get_upload_path, blank=True, null=True)

I am getting the following error when I try to add an Employee via the admin interface:

AttributeError at /admin/employees/employee/add/

'Employee' object has no attribute 'user'

I don't know where this error is stemming from. I took the get_upload_path function from the official Django FIleField documentation.

Any ideas as to what is going on here?

Best,
Mislav
Message has been deleted

coolguy

unread,
Sep 12, 2020, 3:57:21 PM9/12/20
to Django users
I wanted to see your model to understand but i realized after my last post that you are using "instance.user.id" while your employee instance does not have user field. (had to go somewhere urgently) >>> return "employees/media/users/{0}/profile_picture.{1}".format(instance.user.id, extension)  

change it as follow and remove user from it.
return "employees/media/users/{0}/profile_picture.{1}".format(instance.id, extension)  

It should work...

On Saturday, September 12, 2020 at 3:17:28 PM UTC-4 mislav....@gmail.com wrote:
coolguy here is the complete Employee model:

class Employee(models.Model): #TODO: Double-check this
    username = models.CharField(max_length=50, unique=True)
    email = models.EmailField()
    password = models.CharField(max_length=50)
    first_name = models.CharField(max_length=150)
    last_name = models.CharField(max_length=100)
    website = models.URLField(max_length=200, blank=True)

    profile_picture = models.ImageField(upload_to=get_upload_path, blank=True, null=True)
   
    def __str__(self):
        return str(self.first_name) + str(self.last_name)


Why do I need the foreign key to User in the first place? I don't recall seing the foreign key to User in any one of the tutorials.
Message has been deleted
Message has been deleted

coolguy

unread,
Sep 13, 2020, 12:11:18 PM9/13/20
to Django users
not sure about the purpose of showing that example in Django documentation while its comments are clear that "object will not have been saved to the database yet, so if it uses the default AutoField, it might not yet have a value for its primary key field."

so that's the reason for having None with file path.

In this approach what i can see is to save the employee first without file and then edit and select the file.

I would not use this approach and rather keep it simple as followed:

photo = models.ImageField(upload_to='employee/%Y/%m/%d/', blank=True)

On Sunday, September 13, 2020 at 5:41:18 AM UTC-4 mislav....@gmail.com wrote:
Hey coolguy,

thanks for responding. After I changed that line as you suggested that error is solved, but when I add the user through the admin interface, I get None as the ID (the folder that gets created in the /media/users is titled None). I'm not sure if this is expected behavior.

I haven't added the registration or the login yet, so maybe the ID gets a value when someone is actually registering.

Let me know.

Best,
Mislav
Message has been deleted

coolguy

unread,
Sep 14, 2020, 1:45:36 PM9/14/20
to Django users
If i had to stick to your code then i would save the file with instance.username. username is available and folder would be readable as well.

However, as i mentioned in my last reply and seems you agreed, save/register the employee and then have employee upload the picture.

On Monday, September 14, 2020 at 8:20:21 AM UTC-4 mislav....@gmail.com wrote:
What I can do is first register the employee via a register form, then once he logs in ask him/her for the profile picture. Can I do that this way?

If I do it this way, I don't have to change my model in any way.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages