本文整理汇总了Python中django.core.files.uploadedfile.InMemoryUploadedFile.open方法的典型用法代码示例。如果您正苦于以下问题:Python InMemoryUploadedFile.open方法的具体用法?Python InMemoryUploadedFile.open怎么用?Python InMemoryUploadedFile.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.files.uploadedfile.InMemoryUploadedFile
的用法示例。
在下文中一共展示了InMemoryUploadedFile.open方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _file_save
# 需要导入模块: from django.core.files.uploadedfile import InMemoryUploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.InMemoryUploadedFile import open [as 别名]
def _file_save(self, new_pil_obj, version, format, model_instance, file):
"""Save our version files to disk"""
img_version_buffer = StringIO.StringIO()
new_pil_obj.save(img_version_buffer, format)
img_version_buffer = InMemoryUploadedFile(
file=img_version_buffer,
field_name=None,
name='foo',
content_type='image/%s' % format,
size=img_version_buffer.len,
charset=None)
img_version_buffer.open() # reopen just in case, which sets Django's File-like object to seek=0
filename = version['upload_to'](model_instance, file.name)
# delete first to prevent save() from possibily creating a new uniquely named file
# we want to replace the file, which save() may not do natively
file.field.storage.delete(filename)
file.field.storage.save(filename, img_version_buffer)
示例2: test_open_resets_file_to_start_and_returns_context_manager
# 需要导入模块: from django.core.files.uploadedfile import InMemoryUploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.InMemoryUploadedFile import open [as 别名]
def test_open_resets_file_to_start_and_returns_context_manager(self):
uf = InMemoryUploadedFile(StringIO('1'), '', 'test', 'text/plain', 1, 'utf8')
uf.read()
with uf.open() as f:
self.assertEqual(f.read(), '1')