#35658: [Bug] InMemoryFileNode has no attribute "name"
-------------------------+----------------------------------------
Reporter: David | Type: Bug
Status: new | Component: Core (Other)
Version: 4.2 | Severity: Normal
Keywords: storage | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------+----------------------------------------
When copying a file using the `InMemoryStorage` into an other model by
using:
{{{#!python
# models.py
class MyModel(models.Model):
attachment = models.FileField(...)
# script.py
first_obj = MyModel.objects.create(attachment=ContentFile(b'content',
'myfile.txt')
second_obj = MyModel.objects.create(attachment=first_obj.attachment.file)
}}}
An excetption is raised:
{{{
instance = <[AttributeError("'InMemoryFileNode' object has no attribute
'name'") raised in repr()] Attachment object at 0x70e62f1a0ee0>
using = <[AttributeError("'InMemoryFileNode' object has no attribute
'name'") raised in repr()] Attachment object at 0x70e62f1a0ee0>
def fields_for_model_instance(instance, using=None):
'''
Yields (name, descriptor) for each file field given an
instance
Can use the `using` kwarg to change the instance that the
`FieldFile`
will receive.
'''
if using is None:
using = instance
model_name = get_model_name(instance)
deferred_fields = instance.get_deferred_fields()
for field_name in get_fields_for_model(model_name,
exclude=deferred_fields):
fieldfile = getattr(instance, field_name, None)
> yield field_name, fieldfile.__class__(using, fieldfile.field,
fieldfile.name)
E AttributeError: 'NoneType' object has no attribute 'field'
deferred_fields = set()
field_name = 'attachment'
fieldfile = None
instance = <[AttributeError("'InMemoryFileNode' object has no attribute
'name'") raised in repr()] Attachment object at 0x70e62f1a0ee0>
model_name = 'tickets.attachment'
using = <[AttributeError("'InMemoryFileNode' object has no attribute
'name'") raised in repr()] Attachment object at 0x70e62f1a0ee0>
../.venv/lib/python3.10/site-packages/django_cleanup/cache.py:86:
AttributeError
}}}
This may be caused by the fact that `InMemoryFileNode` inheriths from
`ContentFile` but does not use the name attribute nor uses the base class
`__init__` method:
https://github.com/django/django/blob/509763c79952cde02d9f5b584af4278bdbed77b2/django/core/files/storage/memory.py#L48-L52
This resulting in a partially initialized object without an important
property.
--
Ticket URL: <https://code.djangoproject.com/ticket/35658>
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 on the web visit
https://groups.google.com/d/msgid/django-updates/010701912360bf24-3381e1d1-b841-4e97-bda1-8588b9bfa253-000000%40eu-central-1.amazonses.com.