Search code examples
pythondjangofilefielddjango-storage

Removing default file names in Django-Storages S3


I'm using django-storages with amazon S3, and uploading image files with:

models.ImageField(upload_to="img=%Y-%m-%d", max_length=256, blank=True, null=True)

When the files are uploaded to S3 however, it has the original file name attached at the end. How do I get rid of that and replace it, with say some random hash instead?


Solution

  • Supply a callable instead of a string to upload_to. The callable will be passed the instance being saved, and the filename, and will have to return the full path, including the filename -- so you can choose not to use the original filename. (You'll have to call strftime yourself with datetime.date.today(), however).