[Django] #32845: Names of uploaded files cannot be modified.

6 views
Skip to first unread message

Django

unread,
Jun 13, 2021, 1:33:22 PM6/13/21
to django-...@googlegroups.com
#32845: Names of uploaded files cannot be modified.
------------------------------------------------+------------------------
Reporter: Jacob Fredericksen | Owner: nobody
Type: Bug | Status: new
Component: File uploads/storage | Version: 3.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
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:

{{{
...
new_filename = 'sources/new_filename.pdf
initial_filepath = model_instance.file.path
assert os.path.exists(initial_filepath)
new_filepath = os.path.join(settings.MEDIA_ROOT, new_filename)
input(
f'{model_instance.file.name} ---> {new_filename}\n'
f'{initial_filepath} ---> {new_filepath} '
)
if os.path.exists(new_filepath):
raise Exception(f'{new_filepath} already exists.')
f.file.name = new_filename
assert os.path.exists(initial_filepath)
os.rename(initial_filepath, new_filepath)
sleep(5)
assert os.path.exists(new_filepath)
assert not os.path.exists(initial_filepath)
assert model_instance.file.name == new_filename
input('Continue? ')
model_instance.save()
model_instance.refresh_from_db()
assert model_instance.file.path == new_filepath,
f'{model_instance.file.path} != {new_filepath}'
print()
}}}


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. This would seem to indicate that contrary to the docs,
in order to rename a file that is associated with a model instance (as an
ImageField or FileField), it is not sufficient to update
model_instance.file.name and then use the os module to rename the file.

--
Ticket URL: <https://code.djangoproject.com/ticket/32845>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 13, 2021, 1:34:30 PM6/13/21
to django-...@googlegroups.com
#32845: Names of uploaded files cannot be modified.
-------------------------------------+-------------------------------------

Reporter: Jacob Fredericksen | Owner: nobody
Type: Bug | Status: new
Component: File | Version: 3.2
uploads/storage |
Severity: Normal | Resolution:

Keywords: | 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 Jacob Fredericksen:

Old description:

New description:


This does not work.


Result:

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32845#comment:1>

Django

unread,
Jun 13, 2021, 1:40:02 PM6/13/21
to django-...@googlegroups.com
#32845: Names of uploaded files cannot be modified.
-------------------------------------+-------------------------------------

Reporter: Jacob Fredericksen | Owner: nobody
Type: Bug | Status: new
Component: File | Version: 3.2
uploads/storage |
Severity: Normal | Resolution:
Keywords: | 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 Jacob Fredericksen:

Old description:

> According to the Django docs

New description:

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. This would seem to indicate that contrary to the docs,
in order to rename a file that is associated with a model instance (as an
ImageField or FileField), it is not sufficient to update
model_instance.file.name and then use the os module to rename the file.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32845#comment:2>

Django

unread,
Jun 13, 2021, 1:41:20 PM6/13/21
to django-...@googlegroups.com
#32845: Names of uploaded files cannot be modified.
-------------------------------------+-------------------------------------

Reporter: Jacob Fredericksen | Owner: nobody
Type: Bug | Status: new
Component: File | Version: 3.2
uploads/storage |
Severity: Normal | Resolution:
Keywords: | 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 Jacob Fredericksen:

Old description:

> 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. This would seem to indicate that contrary to
> the docs, in order to rename a file that is associated with a model
> instance (as an ImageField or FileField), it is not sufficient to update
> model_instance.file.name and then use the os module to rename the file.

New description:

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.

--

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

Django

unread,
Jun 14, 2021, 12:27:34 PM6/14/21
to django-...@googlegroups.com
#32845: Names of uploaded files cannot be modified.
-------------------------------------+-------------------------------------

Reporter: Jacob Fredericksen | Owner: nobody
Type: Bug | Status: new
Component: File | Version: 3.2
uploads/storage |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Bal Krishna Jha):

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

--
Ticket URL: <https://code.djangoproject.com/ticket/32845#comment:4>

Django

unread,
Jun 15, 2021, 3:57:45 AM6/15/21
to django-...@googlegroups.com
#32845: Names of uploaded files cannot be modified.
-------------------------------------+-------------------------------------

Reporter: Jacob Fredericksen | Owner: nobody
Type: Bug | Status: closed

Component: File | Version: 3.2
uploads/storage |
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* status: new => closed
* resolution: => invalid


Comment:

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

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

Reply all
Reply to author
Forward
0 new messages