Hi,
I have followed the documentation here to have a storage backend defined in STORAGES setting:
STORAGES = {
# Default storage that are required by django
"default": {
"BACKEND": "django.core.files.storage.FileSystemStorage",
},
"staticfiles": {
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
},
# Our specific storages
"custom": {
"BACKEND": "path.to.MyCustomStorage",
},
}
The documentation also indicate that to allow @override_settings STORAGES during tests, the backend must be resolved using a LazyObject in model field so it is not resolved during django’s start process:
class BlopStorage(LazyObject):
def _setup(self):
self._wrapped = storages["custom"]
blop_storage = BlopStorage()
class MyModel(models.Model):
blop_file = models.FileField(
storage=blop_storage,
...
)
But it seems the storage is still resolved at boot time. I’ve debugged here and it seems the function is triggered in FileField.__init__ on self.storage = storage or default_storage line.
So later when tests ran, overriding STORAGES settings doesn’t change the model field storage.
Did I miss something ?
Is the documentation suggestion doesn’t works ?
Any idea how to make it works or allow overriding STORAGES settings ?
EDIT:
After more tests, I confirm that self.storage = storage or default_storage is triggering __bool__ on
LazyObject which force resolving the backend.
A workaround would be to add dumb __bool__ to the LazyObject.
def __bool__(self) -> bool:
return True
But the best fix would maybe to change how django check storage by :
self.storage = storage if storage is not None else default_storage
Thanks