file_content = my_model.my_image.read()
file_name = my_model.my_image.name
# Renaming the file
my_model.my_image = my_model.my_image.save('new_file_name', ContentFile(file_content))
# If you want to delete the original file afterwards:
from django.core.files.storage import default_storage
default_storage.delete(file_name)
不应该使用
os
来移动文件,而应该使用storage要移动文件,可以在
FileField
(或者在本例中是ImageField
)上使用read()
方法(最后使用storage
删除旧文件)。相关问题 更多 >
编程相关推荐