Re: [Django] #35658: InMemoryFileNode object has no attribute "name"

24 views
Skip to first unread message

Django

unread,
Aug 7, 2024, 7:48:58 AM8/7/24
to django-...@googlegroups.com
#35658: InMemoryFileNode object has no attribute "name"
-------------------------------------+-------------------------------------
Reporter: David | Owner: Lucas
| Esposito
Type: Bug | Status: assigned
Component: File | Version: 4.2
uploads/storage |
Severity: Normal | Resolution:
Keywords: storage | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Lucas Esposito):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/35658#comment:8>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 7, 2024, 9:01:45 AM8/7/24
to django-...@googlegroups.com
#35658: InMemoryFileNode object has no attribute "name"
-------------------------------------+-------------------------------------
Reporter: David | Owner: Lucas
| Esposito
Type: Bug | Status: closed
Component: File | Version: 4.2
uploads/storage | Resolution:
Severity: Normal | worksforme
Keywords: storage | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* resolution: => worksforme
* status: assigned => closed

Comment:

Hi David, I have not been able to replicate the error on main or Django
4.2
{{{#!python
@override_settings(
STORAGES={"default": {"BACKEND":
"django.core.files.storage.InMemoryStorage"}}
)
class FileFieldModelTest(TestCase):
def test_repr(self):
test = FileFieldModel.objects.create(
file=ContentFile(b'content', 'myfile.txt')
)
self.assertEqual(repr(test.file), "<FieldFile: myfile.txt>")
}}}
Can you share the full traceback or give more instructions on how to
replicate the error?
--
Ticket URL: <https://code.djangoproject.com/ticket/35658#comment:9>

Django

unread,
Aug 7, 2024, 9:12:01 AM8/7/24
to django-...@googlegroups.com
#35658: InMemoryFileNode object has no attribute "name"
-------------------------------------+-------------------------------------
Reporter: David | Owner: Lucas
| Esposito
Type: Bug | Status: new
Component: File | Version: 4.2
uploads/storage |
Severity: Normal | Resolution:
Keywords: storage | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* resolution: worksforme =>
* status: closed => new

Comment:

Ah I found an error
{{{#!python
@override_settings(
STORAGES={"default": {"BACKEND":
"django.core.files.storage.InMemoryStorage"}}
)
class FileFieldModelTest(TestCase):
def test_repr(self):
test = FileFieldModel()
test.file.save(None, ContentFile(b'content', 'myfile.txt'))
self.assertEqual(repr(test.file), "<FieldFile: myfile.txt>")
}}}

Breaks with
{{{
Traceback (most recent call last):
File "test-django-package/mysite/app3/tests.py", line 13, in test_repr
test.file.save(None, ContentFile(b'content', 'myfile.txt'))
File "test-django-package/venv/lib/python3.10/site-
packages/django/db/models/fields/files.py", line 92, in save
name = self.field.generate_filename(self.instance, name)
File "test-django-package/venv/lib/python3.10/site-
packages/django/db/models/fields/files.py", line 335, in generate_filename
filename = posixpath.join(dirname, filename)
File "/usr/lib/python3.10/posixpath.py", line 90, in join
genericpath._check_arg_types('join', a, *p)
File "/usr/lib/python3.10/genericpath.py", line 152, in _check_arg_types
raise TypeError(f'{funcname}() argument must be str, bytes, or '
TypeError: join() argument must be str, bytes, or os.PathLike object, not
'NoneType'
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35658#comment:10>

Django

unread,
Aug 7, 2024, 9:27:10 AM8/7/24
to django-...@googlegroups.com
#35658: InMemoryFileNode object has no attribute "name"
-------------------------------------+-------------------------------------
Reporter: David | Owner: Lucas
| Esposito
Type: Bug | Status: new
Component: File | Version: 4.2
uploads/storage |
Severity: Normal | Resolution:
Keywords: storage | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

Comment:

Still don't get the error as reported in the ticket so further details
would be welcome
--
Ticket URL: <https://code.djangoproject.com/ticket/35658#comment:11>

Django

unread,
Aug 7, 2024, 11:28:54 AM8/7/24
to django-...@googlegroups.com
#35658: InMemoryFileNode object has no attribute "name"
-------------------------------------+-------------------------------------
Reporter: David | Owner: Lucas
| Esposito
Type: Bug | Status: new
Component: File | Version: 4.2
uploads/storage |
Severity: Normal | Resolution:
Keywords: storage | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Lucas Esposito):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/35658#comment:12>

Django

unread,
Aug 8, 2024, 5:46:26 AM8/8/24
to django-...@googlegroups.com
#35658: InMemoryFileNode object has no attribute "name"
-------------------------------------+-------------------------------------
Reporter: David | Owner: Lucas
| Esposito
Type: Bug | Status: new
Component: File | Version: 4.2
uploads/storage |
Severity: Normal | Resolution:
Keywords: storage | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by David):

Replying to [comment:11 Sarah Boyce]:
> Still don't get the error as reported in the ticket so further details
would be welcome

Sorry I have miss-interpreted a stack trace of an other error coming from
pytest and pasted the wrong part

The stacktrace frame showed up
{{{
instance = <[AttributeError("'InMemoryFileNode' object has no attribute
'name'") raised in repr()] MyModel
}}}

Because the model uses `attachment.name` in its `__str__` method.

Whatching back the behaviour now I have isolated a test which fails:

{{{#!python

obj = MyModel.objects.create(attachment=ContentFile(b'content',
'myfile.txt'))
obj2 = MyModel.objects.create(attachment=obj.attachment.file)
}}}

Which raises the following stacktrace:

{{{
Traceback (most recent call last):
File "<workspace>/db_35658/db_35658/tests.py", line 24, in
test_copy_file
obj2 = MyModel.objects.create(attachment=obj.attachment.file)
File "<workspace>/db_35658/.venv/lib/python3.10/site-
packages/django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "<workspace>/db_35658/.venv/lib/python3.10/site-
packages/django/db/models/query.py", line 658, in create
obj.save(force_insert=True, using=self.db)
File "<workspace>/db_35658/.venv/lib/python3.10/site-
packages/django/db/models/base.py", line 814, in save
self.save_base(
File "<workspace>/db_35658/.venv/lib/python3.10/site-
packages/django/db/models/base.py", line 877, in save_base
updated = self._save_table(
File "<workspace>/db_35658/.venv/lib/python3.10/site-
packages/django/db/models/base.py", line 1020, in _save_table
results = self._do_insert(
File "<workspace>/db_35658/.venv/lib/python3.10/site-
packages/django/db/models/base.py", line 1061, in _do_insert
return manager._insert(
File "<workspace>/db_35658/.venv/lib/python3.10/site-
packages/django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "<workspace>/db_35658/.venv/lib/python3.10/site-
packages/django/db/models/query.py", line 1805, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "<workspace>/db_35658/.venv/lib/python3.10/site-
packages/django/db/models/sql/compiler.py", line 1821, in execute_sql
for sql, params in self.as_sql():
File "<workspace>/db_35658/.venv/lib/python3.10/site-
packages/django/db/models/sql/compiler.py", line 1745, in as_sql
value_rows = [
File "<workspace>/db_35658/.venv/lib/python3.10/site-
packages/django/db/models/sql/compiler.py", line 1746, in <listcomp>
[
File "<workspace>/db_35658/.venv/lib/python3.10/site-
packages/django/db/models/sql/compiler.py", line 1747, in <listcomp>
self.prepare_value(field, self.pre_save_val(field, obj))
File "<workspace>/db_35658/.venv/lib/python3.10/site-
packages/django/db/models/sql/compiler.py", line 1695, in pre_save_val
return field.pre_save(obj, add=True)
File "<workspace>/db_35658/.venv/lib/python3.10/site-
packages/django/db/models/fields/files.py", line 314, in pre_save
file = super().pre_save(model_instance, add)
File "<workspace>/db_35658/.venv/lib/python3.10/site-
packages/django/db/models/fields/__init__.py", line 932, in pre_save
return getattr(model_instance, self.attname)
File "<workspace>/db_35658/.venv/lib/python3.10/site-
packages/django/db/models/fields/files.py", line 200, in __get__
file_copy = self.field.attr_class(instance, self.field, file.name)
AttributeError: 'InMemoryFileNode' object has no attribute 'name'
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35658#comment:13>

Django

unread,
Aug 9, 2024, 5:32:56 AM8/9/24
to django-...@googlegroups.com
#35658: InMemoryFileNode object has no attribute "name"
-------------------------------------+-------------------------------------
Reporter: David | Owner: Lucas
| Esposito
Type: Bug | Status: new
Component: File | Version: 4.2
uploads/storage |
Severity: Normal | Resolution:
Keywords: storage | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Accepted => Ready for checkin

Comment:

Thank you for the follow up David - that helped a lot!
--
Ticket URL: <https://code.djangoproject.com/ticket/35658#comment:14>

Django

unread,
Aug 9, 2024, 6:27:25 AM8/9/24
to django-...@googlegroups.com
#35658: InMemoryFileNode object has no attribute "name"
-------------------------------------+-------------------------------------
Reporter: David | Owner: Lucas
| Esposito
Type: Bug | Status: closed
Component: File | Version: 4.2
uploads/storage |
Severity: Normal | Resolution: fixed
Keywords: storage | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

* resolution: => fixed
* status: new => closed

Comment:

In [changeset:"f16a9a556fb4225f9094048614f4fcec3db7e067" f16a9a55]:
{{{#!CommitTicketReference repository=""
revision="f16a9a556fb4225f9094048614f4fcec3db7e067"
Fixed #35658 -- Initialized InMemoryFileNode instances with a name.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35658#comment:15>

Django

unread,
Aug 15, 2024, 2:52:29 AM8/15/24
to django-...@googlegroups.com
#35658: InMemoryFileNode object has no attribute "name"
-------------------------------------+-------------------------------------
Reporter: David | Owner: Lucas
| Esposito
Type: Bug | Status: closed
Component: File | Version: 4.2
uploads/storage |
Severity: Normal | Resolution: fixed
Keywords: storage | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce <42296566+sarahboyce@…>):

In [changeset:"a57596e443ecb67140e1a9fc0f0e87446b2d0174" a57596e]:
{{{#!CommitTicketReference repository=""
revision="a57596e443ecb67140e1a9fc0f0e87446b2d0174"
Refs #35658 -- Fixed
test_create_file_field_from_another_file_field_in_memory_storage when run
in reverse.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35658#comment:16>
Reply all
Reply to author
Forward
0 new messages