How can I replace/override an uploaded file?

非 Y 不嫁゛ 提交于 2019-12-12 13:28:04

问题


I want to be able to upload a file and on each upload to override/replace the existing file with the newest version.

from django.core.files.storage import FileSystemStorage  
fs = FileSystemStorage(location='C:/temp', base_url='/attachments')      
class Import(models.Model):  
    file = models.FileField(upload_to='data', storage=fs)

回答1:


I don't know if this is the best approach, but the following lines helped me override/replace the existing file.

upload_dir_path = Setting.objects.get(entry__exact='upload_path').value
delete_files(upload_dir_path)
upload = form.save(commit=False)
upload.file.storage.location = upload_dir_path            
upload = form.save()


来源:https://stackoverflow.com/questions/4253860/how-can-i-replace-override-an-uploaded-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!