[英]Django Custom File Storage system
I have a custom storage我有一个自定义存储
import os
from django.core.files.storage import Storage
class AlwaysOverwriteFileSystemStorage(Storage):
def get_available_name(self, name):
"""
Directly Returns a filename that's
from what user input.
"""
if self.exists(name):
# Remove the existing file
os.remove(name)
# Return the input name as output
return name
I want to know where should I put his AlwaysOverwriteFileSystemStorage.py
file and how should by settings.py define DEFAULT_FILE_STORAGE
我想知道我应该把他的AlwaysOverwriteFileSystemStorage.py
文件放在AlwaysOverwriteFileSystemStorage.py
以及如何通过settings.py定义DEFAULT_FILE_STORAGE
My Django root folder is /home/username/webapp我的 Django 根文件夹是/home/username/webapp
When I put DEFAULT_FILE_STORAGE = 'site.storage.AlwaysOverwriteFileSystemStorage'
, it returns an当我放置DEFAULT_FILE_STORAGE = 'site.storage.AlwaysOverwriteFileSystemStorage'
,它返回一个
Error错误
Error importing storage module site.storage: "No module named storage"导入存储模块 site.storage 时出错:“没有名为存储的模块”
I am not familiar with Python/Django, any help will be much appreciated.我不熟悉 Python/Django,任何帮助将不胜感激。 Thank you.谢谢。
You don't need to put anything in your settings.py
.您无需在settings.py
放置任何内容。 Just use it directly in your model.只需直接在您的模型中使用它。 For example, create storage.py
wherever your app is located and put OverwriteStorage()
in it.例如,在您的应用程序所在的任何位置创建storage.py
并将OverwriteStorage()
放入其中。 Then, your model could look like this:然后,您的模型可能如下所示:
from storage import OverwriteStorage
...
class MyModel(models.Model):
...
image = ImageField(upload_to='images', storage=OverwriteStorage())
I am also using a custom storage system to overwrite existing files.我还使用自定义存储系统来覆盖现有文件。 My storage.py
looks like this:我的storage.py
看起来像这样:
from django.core.files.storage import FileSystemStorage
class OverwriteStorage(FileSystemStorage):
"""
Returns same name for existing file and deletes existing file on save.
"""
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
You can put it anywhere, but you should point the path to there in the settings.py你可以把它放在任何地方,但你应该在 settings.py 中指向那里的路径
You can put this storage.py
file in the root folder (the one that has manage.py
) and point the path as storage.AlwaysOverwriteFileSystemStorage
您可以将此storage.py
文件放在根文件夹(有manage.py
文件夹中)并将路径指向storage.AlwaysOverwriteFileSystemStorage
I try to make this, but don`t work for me, until I make this on my storage.py :我试着做这个,但不适合我,直到我在我的 storage.py 上做这个:
import os
from django.conf import settings
from django.core.files.storage import FileSystemStorage
class MyFileSystemStorage(FileSystemStorage):
def get_available_name(self, name, *args, **kwargs):
if self.exists(name):
os.remove(os.path.join(settings.MEDIA_ROOT, name))
return super().get_available_name(name, *args, **kwargs)
No other changes was required.不需要其他更改。
Another way yo do this:另一种方式你这样做:
from django.core.files.storage import FileSystemStorage
class AvatarStorage(FileSystemStorage):
def get_available_name(self, name, *args, **kwargs):
self.delete(name)
return super().get_available_name(name, *args, **kwargs)
The delete method check if the file exists. delete 方法检查文件是否存在。 So these 3 line are enough.所以这3行就足够了。 I hope this help somebody我希望这有助于某人
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.