Figures, I solved the problem after posting the question. With a bit more 
searching and reading I found a rather simple solution to this.

Changing the upload_to= to use a callable works. The callable needs to have 
two parameters instance and filename. changing the file name using a uuid 
seems to do the trick for now. I have included my solution just incase 
anyone else wants to do the same thing. :)

def generate_random_name(instance, filename):
    filename = f'{uuid4().hex}.jpg'
    return os.path.join('profile_pics', filename)


On Monday, September 7, 2020 at 2:07:38 PM UTC-7 Mike Walker wrote:

> I posted this over in the django-imagekit group before I noticed that I am 
> unlikely to get any response over there due to activity level. So I am now 
> over here to ask this goup.
>
> I am using ProcessedImageField to resize images on upload in my user 
> Profile model. The resizing part is working great and the resized image is 
> successfully saved to my s3 bucket.
>
> However what I would like to add is the ability to give the file a random 
> name before upload. This would hopefully avoid the file name collisions 
> issue. I am not sure how approach this yet, could someone point me in the 
> right direction here?
>
> I have included the code for my current model below.
>
> class Profile(models.Model):
>     user = models.OneToOneField(User, on_delete=models.CASCADE)
>
>     image = ProcessedImageField(default='default.jpg', 
> upload_to='profile_pics',
>                                 processors=[ResizeToFill(200, 200)],
>                                 format='JPEG', options={'quality': 60})
>
>     def __str__(self):
>         return f'{self.user.username} Profile'
>

-- 
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 django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/aabea96b-58bf-4850-ae3c-e5e116b3a684n%40googlegroups.com.

Reply via email to