#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>