I would like to keep the original file name of an UploadedFile
in Django that has its location stored in a FileField
. Right now I am observing that if two files have the same name, the first file uploaded keeps its original name but the second time a file with that name is uploaded, it has a random string appended to make the file name unique. One solution is to add an additional field to the model: Django: How to save original filename in FileField? or Saving Original File Name in Django with FileField but these solutions seem suboptimal as they require changing the Model
fields.
An alternative would be to prepend a random directory path to the front of the file make sure that in a given directory the file name is unique and allowing the basename
to remain unchanged. One way to do this would be to pass in a callable upload_to
that does just that. Another option would be to subclass FileField
and override get_filename
to not strip the input filename to the basename
allowing the caller to pass in a filename with a prepended path. The latter option is not ideal if I want to use an ImageField
as I would have to subclass that as well.