如何在内存中创建文件(而不是上传的文件)并通过Django default_storage保存到FileField?

时间:2018-07-21 21:11:38

标签: django python-3.x django-storage pyexcel

如果要像这样在内存中创建类似文件的csv对象:

output_stream = io.StringIO()
sheet = pyexcel.get_sheet(records=data)
sheet.save_to_memory(file_type='csv', stream=output_stream)

如何使用Django将output_stream中的对象之类的文件保存到default_storage后端上的文件中?

class Example(models.Model):
    model_file = models.FileField(upload_to='', max_length=255, blank=True, null=True)

我尝试过类似的事情:

self.model_file.save(filename, ContentFile(output_stream.read()))

但是出现以下错误:

"TypeError: ('data must be bytes, received', <class 'str'>)"

pyexcel仅支持CSV类型文件的io.StringIO流。

跟踪:

...
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\files\storage.py", line 49, in save
    return self._save(name, content)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\storages\backends\gcloud.py", line 167, in _save
    content_type=file.mime_type)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\google\cloud\storage\blob.py", line 1034, in upload_from_file
    size, num_retries, predefined_acl)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\google\cloud\storage\blob.py", line 947, in _do_upload
    size, num_retries, predefined_acl)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\google\cloud\storage\blob.py", line 759, in _do_multipart_upload
    transport, data, object_metadata, content_type)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\google\resumable_media\requests\upload.py", line 94, in transmit
    data, metadata, content_type)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\google\resumable_media\_upload.py", line 270, in _prepare_request
    raise TypeError(u'`data` must be bytes, received', type(data))
TypeError: ('`data` must be bytes, received', <class 'str'>)

1 个答案:

答案 0 :(得分:2)

您可以从StringIO中读取内容,并转换为utf8编码的字节。

self.model_file.save(filename, ContentFile(output_stream.getvalue().encode()))

getvalue()read()类似,但是无论当前流位置如何,您都将始终获取StringIO的全部内容。使用read(),您可能需要使用seek(0)进行倒带。