我正在尝试使用Django中的文件存储。一切都很好,但我想我的保存方法中有一件事。我有一个带有FileField
的模型
download_url = models.FileField(verbose_name = 'Konfig', upload_to = file_path, storage = OverwriteStorage())
在我的模型中的这个方法中,我创建了file_path
我使用的文件存储方法是在我的存储.py我在我的模型.py在
from django.core.files.storage import FileSystemStorage
class OverwriteStorage(FileSystemStorage):
def _save(self, name, content):
if self.exists(name):
self.delete(name)
return super(OverwriteStorage, self)._save(name, content)
def get_available_name(self, name):
return name
现在,当我从django在管理界面中创建一个新文件时,它成功地上传了文件,用正确的文件路径创建了一个数据库条目,但是它无法创建正确的路径。当我的文件名为foo时,路径如下所示:
cwd/files/foo/foo
如果它的名字是条形图.txt如下所示:
cwd/files/bar.txt/bar.txt
我不希望django基于文件名创建子目录。你们能帮帮我吗?在
我很确定您必须将save函数从“save”重命名为“\u save”。在
在Super调用中,您使用了.\u save,它与上面的save函数不同。在
你可以读到很多关于超级here
相关问题 更多 >
编程相关推荐