Reputation: 5074
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:
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.
Upvotes: 3
Views: 3662
Reputation: 1103
I managed to delete a media file easily using Django FileSystemStorage in Django==3.2. All you need to do is pass the absolute path of the file you want to delete into fs.delete(absolute_path) The absolute path can also be easily obtained from fs.path(filename)
from django.core.files.storage import FileSystemStorage
img_file = request.FILES['file']
fs = FileSystemStorage()
filename = fs.save(img_file.name, img_file)
uploaded_file_path = fs.path(filename)
"""
now that you have the absolute path
you can just pass this to fs.delete(absolute_path) anytime
to delete that particular media file like so,
"""
fs.delete(uploaded_file_path)
Upvotes: 3
Reputation: 2166
Do it like this:
class FileUpload(models.Model):
docFile = models.FileField(upload_to=path_file())
And here is the function that handles your upload:
def path_file():
def wrapper(user, filename):
file_upload_dir = os.path.join(settings.MEDIA_ROOT, 'file_upload')
if os.path.exists(file_upload_dir):
import shutil
shutil.rmtree(file_upload_dir)
return os.path.join(file_upload_dir, filename)
return wrapper
There you go. So the idea is simple. Any file that get uploaded will arrive in a well defined directory. Delete and recreate this directory every time an upload happens
I also suggest you that you rename the name of the uploaded file to whatever you want. Something like single_file.extension
Upvotes: 2