我希望能够上传文件并在每次上传时覆盖/替换最新版本的现有文件。
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)
答案 0 :(得分:1)
我不知道这是否是最佳方法,但以下几行帮助我覆盖/替换现有文件。
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()