Opened 3 years ago

Closed 3 years ago

#32845 closed Bug (invalid)

Names of uploaded files cannot be modified.

Reported by: Jacob Fredericksen Owned by: nobody
Component: File uploads/storage Version: 3.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Jacob Fredericksen)

According to the Django docs (https://docs.djangoproject.com/en/3.2/topics/files/), I should be able to update the name of an uploaded file like so:

import os
from django.conf import settings
initial_path = car.photo.path
car.photo.name = 'cars/chevy_ii.jpg'
new_path = settings.MEDIA_ROOT + car.photo.name
# Move the file on the filesystem
os.rename(initial_path, new_path)
car.save()
car.photo.path
'/media/cars/chevy_ii.jpg'
car.photo.path == new_path
True

This does not work.

Here is the script I used to test:

            ...
            initial_filepath = model_instance.file.path
            new_filename = 'sources/new_filename.pdf
            new_filepath = os.path.join(settings.MEDIA_ROOT, new_filename)
            # Verify the initial filepath exists and the new filepath does not yet exist.
            assert os.path.exists(initial_filepath)
            if os.path.exists(new_filepath):
                raise Exception(f'{new_filepath} already exists.')
            print(
                f'{model_instance.file.name} ---> {new_filename}\n'
                f'{initial_filepath} ---> {new_filepath} '
            )

            # Rename the file.
            f.file.name = new_filename
            os.rename(initial_filepath, new_filepath)
            sleep(5)

            # Verify the file was renamed successfully.
            assert os.path.exists(new_filepath)
            assert not os.path.exists(initial_filepath)
            assert model_instance.file.name == new_filename

            input('Continue? ')

            # Save the model instance.
            model_instance.save()
            model_instance.refresh_from_db()

            # Verify the file was renamed successfully.
            assert model_instance.file.path == new_filepath, f'{model_instance.file.path} != {new_filepath}'

Result:

sources/old_filename.pdf ---> sources/new_filename.pdf
/project/media/sources/old_filename.pdf ---> /project/media/sources/new_filename.pdf 
Continue? 
Traceback (most recent call last):
  File "/project/my_script.py", line 108, in <module>
    assert model_instance.file.path == new_filepath, f'{model_instance.file.path} != {new_filepath}'
AssertionError: /project/media/sources/old_filename.pdf != /project/media/sources/new_filename.pdf

I watched the file name (in my file browser) as the script ran. The file name is successfully changed (as demonstrated by the successful assertions above), but it is then reverted to the original file name when the model instance is saved.

Change History (5)

comment:1 by Jacob Fredericksen, 3 years ago

Description: modified (diff)

comment:2 by Jacob Fredericksen, 3 years ago

Description: modified (diff)

comment:3 by Jacob Fredericksen, 3 years ago

Description: modified (diff)

comment:4 by Bal Krishna Jha, 3 years ago

You need to refer model_instance while changing the name.
Replace f.file.name with model_instance.file.name and it should work.

comment:5 by Carlton Gibson, 3 years ago

Resolution: invalid
Status: newclosed

Yes, I suspect this is a usage problem. See TicketClosingReasons/UseSupportChannels. If not, please provide a full minimal example, as a project or a failing test case showing Django is at fault.
Thanks

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