django文件保存遇到相同文件名的时候会自动在后面加下划线,比如:
a.txt重复提交之后会出现a_uysdf.txt,而我们有时业务需要直接覆盖先前的文件,该怎么办呢?
import os from django.core.files.storage import FileSystemStorage class mystorage(FileSystemStorage): def get_available_name(self, name): if self.exists(name): os.remove(os.path.join(settings.MEDIA_ROOT, name)) return name file = models.FileField(upload_to='file/',storage=mystorage())