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 
<http://instance.user.id/>, extension)  

It should work...

On Saturday, September 12, 2020 at 3:17:28 PM UTC-4 [email protected] 
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.
> Dana subota, 12. rujna 2020. u 20:20:11 UTC+2 korisnik coolguy napisao je:
>
>> 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 [email protected] 
>> 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 
>>> <https://docs.djangoproject.com/en/3.1/ref/models/fields/#filefield>.
>>>
>>> Any ideas as to what is going on here?
>>>
>>> Best,
>>> Mislav
>>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8f08f03a-9617-48b9-bfdb-d00e44652cf1n%40googlegroups.com.

Reply via email to