Skip to content

Commit f16a9a5

Browse files
LucasEspositosarahboyce
authored andcommitted
Fixed #35658 -- Initialized InMemoryFileNode instances with a name.
1 parent 69aa13f commit f16a9a5

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

django/core/files/storage/memory.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ class InMemoryFileNode(ContentFile, TimingMixin):
4545
modification, and access times.
4646
"""
4747

48-
def __init__(self, content="", name=""):
49-
self.file = None
48+
def __init__(self, content="", name=None):
49+
super().__init__(content, name)
5050
self._content_type = type(content)
51-
self._initialize_stream()
5251
self._initialize_times()
5352

5453
def open(self, mode):
@@ -142,7 +141,11 @@ def _resolve_child(self, path_segment, create_if_missing, child_cls):
142141
if create_if_missing:
143142
self._update_accessed_time()
144143
self._update_modified_time()
145-
return self._children.setdefault(path_segment, child_cls())
144+
if child_cls is InMemoryFileNode:
145+
child = child_cls(name=path_segment)
146+
else:
147+
child = child_cls()
148+
return self._children.setdefault(path_segment, child)
146149
return self._children.get(path_segment)
147150

148151
def listdir(self):

tests/file_storage/tests.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,23 @@ def test_stringio(self):
10241024
with temp_storage.open("tests/stringio") as f:
10251025
self.assertEqual(f.read(), b"content")
10261026

1027+
@override_settings(
1028+
STORAGES={
1029+
DEFAULT_STORAGE_ALIAS: {
1030+
"BACKEND": "django.core.files.storage.InMemoryStorage"
1031+
}
1032+
}
1033+
)
1034+
def test_create_file_field_from_another_file_field_in_memory_storage(self):
1035+
f = ContentFile("content", "file.txt")
1036+
obj = Storage.objects.create(storage_callable_default=f)
1037+
new_obj = Storage.objects.create(
1038+
storage_callable_default=obj.storage_callable_default.file
1039+
)
1040+
storage = callable_default_storage()
1041+
with storage.open(new_obj.storage_callable_default.name) as f:
1042+
self.assertEqual(f.read(), b"content")
1043+
10271044

10281045
class FieldCallableFileStorageTests(SimpleTestCase):
10291046
def setUp(self):

0 commit comments

Comments
 (0)