This commit is contained in:
khosro 2025-11-17 20:42:14 +02:00 committed by GitHub
commit 4130fe8376
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -292,12 +292,28 @@ need to override the :setting:`STORAGES` setting in tests, you should use a
class MyModel(models.Model):
upload = models.FileField(storage=my_storage)
The ``LazyObject`` delays the evaluation of the storage until it's actually
needed, allowing :func:`~django.test.override_settings` to take effect::
The ``LazyObject`` delays evaluation of the storage until it's actually
needed, allowing :func:`~django.test.override_settings` to take effect. To
make this work, connect to the
:data:`~django.test.signals.setting_changed` signal and reset storages when
``STORAGES`` is overridden::
from django.core.files.storage import default_storage, staticfiles_storage
from django.core.signals import setting_changed
from django.dispatch import receiver
@receiver(setting_changed)
def update_filefield_storage(setting, **kwargs):
if setting == "STORAGES":
default_storage._wrapped = empty
staticfiles_storage._wrapped = empty
And in your tests::
@override_settings(
STORAGES={
"mystorage": {
"default": {
"BACKEND": "django.core.files.storage.InMemoryStorage",
}
}