3

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)
Seitaridis
  • 4,459
  • 9
  • 53
  • 85

1 Answers1

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()
Seitaridis
  • 4,459
  • 9
  • 53
  • 85
  • if anyone interested in this solution, see [Thnee answer here](http://stackoverflow.com/questions/4394194/replacing-a-django-image-doesnt-delete-original) for a more detailled explanation – Guillaume Gendre Feb 04 '13 at 16:10