My goal is that each time a user uploads a file to the media folder, that the previous file is deleted and replaced with the current. Essentially I only want a single file in the folder at a time. I have a working example that overwrites a file within the folder if it contains the same name and I figure that a simple modification to it would yield a solution, but I have't been able to correctly implement the modification.
The code I have to overwrite duplicates is as follows:
Models.py
class OverwriteStorage(FileSystemStorage):
def get_available_name(self, name):
if self.exists(name):
os.remove(os.path.join(settings.MEDIA_ROOT, name))
return name
class FileUpload(models.Model):
docFile = models.FileField(upload_to='Data_Files', storage=OverwriteStorage(), blank=True)
def __str__(self):
return self.docfile.name
@property
def filename(self):
return os.path.basename(self.docFile.name)
Any ideas on modifications or other potential solutions would be greatly appreciated.