[Fixed]-Django – Overwriting files with default_storage.save()

1👍

Take a look at django-storages.

It has an overwrite storage backend that does what you’re looking for. You just need to update your settings.py to use the OverwriteStorage class:

DEFAULT_FILE_STORAGE = 'storages.backends.overwrite.OverwriteStorage'

0👍

You can delete the file and then save, look at this:

from django.core.files.storage import default_storage
from django.core.files.base import ContentFile

file = ContentFile("Some text", name="data.txt")
path = f"/path/to/{file.name}"

if default_storage.exists(path):
    default_storage.delete(path)
    
default_storage.save(path, file)

Leave a comment