【发布时间】:2017-08-10 01:36:00
【问题描述】:
我正在尝试为我的 Django 应用创建一个 custom file storage class,它透明地记录所有保存文件的哈希值。
我的测试存储类非常简单:
from django.core.files.storage import Storage
from django.db.models.fields.files import FieldFile
from utils import get_text_hash
class MyStorage(Storage)
def _save(self, name, content):
if isinstance(content, FieldFile):
raw_content = content.open().read()
else:
raw_content = content
assert isinstance(raw_content, basestring)
print(get_text_hash(raw_content))
return super(MyStorage, self)._save(name, content)
但是,当我尝试在我的应用中保存文件时,出现错误:
'NoneType' object has no attribute 'read'
回溯结束于行:
raw_content = content.open().read()
为什么open() 返回 None 而不是文件句柄?在 Django 存储类中访问原始文件内容的正确方法是什么?
【问题讨论】:
标签: python django django-storage