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

18 views
Skip to first unread message

Django

unread,
Aug 5, 2024, 12:30:34 PM8/5/24
to django-...@googlegroups.com
#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.

Django

unread,
Aug 6, 2024, 4:10:05 AM8/6/24
to django-...@googlegroups.com
#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:
New description:

With [https://github.com/un1t/django-cleanup django-cleanup] installed,
when copying a file using the `InMemoryStorage` into an other model by
--
Ticket URL: <https://code.djangoproject.com/ticket/35658#comment:1>

Django

unread,
Aug 6, 2024, 4:27:11 AM8/6/24
to django-...@googlegroups.com
#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:

New description:

Using the `InMemoryStorage` into an other model by using:

{{{#!python
# models.py

class MyModel(models.Model):
attachment = models.FileField(...)

# script.py

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

An excetption is raised: `AttributeError("'InMemoryFileNode' object has no
attribute 'name'") raised in repr()`


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:2>

Django

unread,
Aug 6, 2024, 9:53:32 PM8/6/24
to django-...@googlegroups.com
#35658: [Bug] InMemoryFileNode has no attribute "name"
------------------------------+------------------------------------------
Reporter: David | Owner: Lucas Esposito
Type: Bug | Status: assigned
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
------------------------------+------------------------------------------
Changes (by Lucas Esposito):

* owner: (none) => Lucas Esposito
* status: new => assigned

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

Django

unread,
Aug 6, 2024, 9:59:27 PM8/6/24
to django-...@googlegroups.com
#35658: [Bug] InMemoryFileNode has no attribute "name"
------------------------------+------------------------------------------
Reporter: David | Owner: Lucas Esposito
Type: Bug | Status: assigned
Component: Core (Other) | Version: 4.2
Severity: Normal | Resolution:
Keywords: storage | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+------------------------------------------
Changes (by Lucas Esposito):

* has_patch: 0 => 1

Comment:

I agree about the need of the issue. I also found other places were the
'name' attribute may be accessed, which would result in a similar error as
described above.

I just made a PR: [https://github.com/django/django/pull/18453]
--
Ticket URL: <https://code.djangoproject.com/ticket/35658#comment:4>

Django

unread,
Aug 6, 2024, 10:00:10 PM8/6/24
to django-...@googlegroups.com
#35658: [Bug] InMemoryFileNode has no attribute "name"
------------------------------+------------------------------------------
Reporter: David | Owner: Lucas Esposito
Type: Bug | Status: assigned
Component: Core (Other) | Version: 4.2
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):

* stage: Unreviewed => Accepted

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

Django

unread,
Aug 7, 2024, 4:18:09 AM8/7/24
to django-...@googlegroups.com
#35658: [Bug] InMemoryFileNode has no attribute "name"
------------------------------+------------------------------------------
Reporter: David | Owner: Lucas Esposito
Type: Bug | Status: assigned
Component: Core (Other) | Version: 4.2
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

--
Ticket URL: <https://code.djangoproject.com/ticket/35658#comment:6>
Reply all
Reply to author
Forward
0 new messages