Opened 4 months ago

Last modified 3 months ago

#35658 closed Bug

[Bug] InMemoryFileNode has no attribute "name" — at Initial Version

Reported by: David Owned by:
Component: File uploads/storage Version: 4.2
Severity: Normal Keywords: storage
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When copying a file using the InMemoryStorage into an other model by using:

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

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top