#36269: Cant override storage used in a FileField other than the default storage
-------------------------------------+-------------------------------------
     Reporter:  emillumine           |                     Type:  Bug
       Status:  new                  |                Component:  File
                                     |  uploads/storage
      Version:  5.2                  |                 Severity:  Normal
     Keywords:  override_settings,   |             Triage Stage:
  storages                           |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 It seems that the default storage and other user defined storages dont
 have the same behaviour in tests.
 When overriding the settings.STORAGES in tests we can easily replace the
 default storage whereas it doesn't work for another user defined storage.
 I tried to reproduce my problem with a minimal code below.

 It may seems successful at first because the first assertion `assert
 storages["other_storage"]._location == "other_location"` is green. But the
 last one `assert test.other_file.storage._location == "other_location"` is
 not. Its value is the one defined in settings.STORAGES.

 I would expect a similar behaviour in both cases.

 {{{
 # settings.py
 STORAGES = {
     "default": {
         "BACKEND": "django.core.files.storage.FileSystemStorage",
     },
     "staticfiles": {
         "BACKEND":
 "django.contrib.staticfiles.storage.StaticFilesStorage",
     },
     "other_storage": {
         "BACKEND": "django.core.files.storage.FileSystemStorage",
     }
 }
 }}}


 {{{
 # test.py

 from django.test import override_settings
 from django.core.files.storage import default_storage
 from django.core.files.storage import storages
 from django.core.files.storage import InMemoryStorage
 from django.db import models
 from django.core.files.base import ContentFile


 def select_other_storage():
     return storages["other_storage"]

 def select_default_storage():
     return default_storage


 class TestModel(models.Model):
     other_file = models.FileField(storage=select_other_storage)
     default_file = models.FileField(storage=select_default_storage)

     class Meta:
         app_label = "test"

 @override_settings(
     STORAGES={
         "default": {
             "BACKEND": "django.core.files.storage.InMemoryStorage",
             "OPTIONS": {
                 "location": "default_location",
                 },
             },
         "other_storage": {
             "BACKEND": "django.core.files.storage.InMemoryStorage",
             "OPTIONS": {
                 "location": "other_location",
                 },
         }
     }
 )
 def test_override_settings():
     assert default_storage._location == "default_location"
     assert storages["other_storage"]._location == "other_location"

     test = TestModel(
         other_file=ContentFile("test data", name="test.pdf"),
         default_file=ContentFile("test data", name="test.pdf"),
     )
     assert isinstance(test.default_file.storage, InMemoryStorage)
     assert test.default_file.storage._location == "default_location"
     # following assertions fail
     assert isinstance(test.other_file.storage, InMemoryStorage)
     assert test.other_file.storage._location == "other_location"
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36269>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070195b3e0e4ce-be68ae5f-8602-4897-a2d4-5704b9bfdfdb-000000%40eu-central-1.amazonses.com.

Reply via email to