﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35658	"[Bug] InMemoryFileNode has no attribute ""name"""	David		"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."	Bug	new	Core (Other)	4.2	Normal		storage		Unreviewed	0	0	0	0	0	0
