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

2020-09-15 Thread Mislav Jurić
Thank you for your suggestion. I will use the username, as it is unique as well. Best, Mislav Dana ponedjeljak, 14. rujna 2020. u 19:45:36 UTC+2 korisnik coolguy napisao je: > If i had to stick to your code then i would save the file with > instance.username. username is available and folder

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

2020-09-14 Thread coolguy
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, Septembe

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

2020-09-14 Thread Mislav Jurić
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. Dana nedjelja, 13. rujna 2020. u 18:11:18 UTC+2 korisnik coolguy napisao je: >

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

2020-09-13 Thread coolguy
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 Non

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

2020-09-13 Thread coolguy
May the purpose of showing that example in Django documentation is something else. If you read the comments there , there is a potential issue that the instance that they suggest to use mostly would have not been persisted in the database if AutoField has been used. so there will be 'no' id as

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

2020-09-13 Thread Mislav Jurić
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 h

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

2020-09-12 Thread coolguy
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)

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

2020-09-12 Thread Mislav Jurić
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_

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

2020-09-12 Thread coolguy
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):

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

2020-09-12 Thread Mislav Jurić
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 attri