#35658: [Bug] InMemoryFileNode has no attribute "name"
------------------------------+--------------------------------------
Reporter: David | Owner: (none)
Type: Bug | Status: new
Component: Core (Other) | Version: 4.2
Severity: Normal | Resolution:
Keywords: storage | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------
Description changed by David:
Old description:
> 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.
New description:
With [https://github.com/un1t/django-cleanup django-cleanup] installed,
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#comment:1>
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/0107019126bceb50-ed0a6999-cd73-41f8-91ee-328dafe948ed-000000%40eu-central-1.amazonses.com.