mirror of
https://github.com/django/django.git
synced 2025-11-18 02:56:45 +00:00
Merge 95d3c51b58 into 1ce6e78dd4
This commit is contained in:
commit
4130fe8376
1 changed files with 19 additions and 3 deletions
|
|
@ -292,12 +292,28 @@ need to override the :setting:`STORAGES` setting in tests, you should use a
|
||||||
class MyModel(models.Model):
|
class MyModel(models.Model):
|
||||||
upload = models.FileField(storage=my_storage)
|
upload = models.FileField(storage=my_storage)
|
||||||
|
|
||||||
The ``LazyObject`` delays the evaluation of the storage until it's actually
|
The ``LazyObject`` delays evaluation of the storage until it's actually
|
||||||
needed, allowing :func:`~django.test.override_settings` to take effect::
|
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(
|
@override_settings(
|
||||||
STORAGES={
|
STORAGES={
|
||||||
"mystorage": {
|
"default": {
|
||||||
"BACKEND": "django.core.files.storage.InMemoryStorage",
|
"BACKEND": "django.core.files.storage.InMemoryStorage",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue