How do you change the filename of a media file (user-uploaded file) in Django after it's been saved?
I understand that if you want to do it as it's being uploaded that you can follow the solution in questions like this, but I'm talking about changing the names of images that are already in the database.
I have tried overriding the name attribute of the ImageFileField
and then saving the model, but this does not affect the file itself. It just breaks the reference because now the ImageFileField
is pointing at the new name but the file still has the old one.
Using the same example model as the linked question:
class SomeModel(models.Model):
title = models.CharField(max_length=100)
video = models.FileField(upload_to='video')
This does NOT work:
>>> for m in SomeModel.objects.all():
... m.video.name = 'new_video_name.avi'
... m.save()
What should I be doing here?