Django文件字段如何填充流io
Django file field how to fill with stream io
我正在尝试在模型中附加一个当前不在磁盘上但存在于内存中的文件。
该模型使用 django-filer,如果我传递一个常规文件它可以工作,使用 io 流它会失败。
def test_save_bytesio():
from PIL import Image
import io
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.core.files.uploadedfile import SimpleUploadedFile
img = Image.new("RGB",(4,4))
thumb_io = io.BytesIO()
img.save(thumb_io, format='JPEG')
# thumb_file = InMemoryUploadedFile(thumb_io, None, '{}_da_ordine.jpg'.format(1), 'image/jpeg',
# thumb_io,None)
thumb_file = DjangoFile(thumb_io.getvalue(), name='gigi')
#thumb_file = DjangoFile(thumb_io, name='gigi')
mymodel = MyModelTosave.objects.create(
name='gigi',
file=thumb_file
)
这是错误堆栈:
return field.pre_save(obj, add=True)
../../.local/share/virtualenvs/--M2Y9QA9/lib/python3.7/site-packages/django/db/models/fields/files.py:288: in pre_save
file.save(file.name, file.file, save=False)
../../.local/share/virtualenvs/--M2Y9QA9/lib/python3.7/site-packages/filer/fields/multistorage_file.py:121: in save
content.seek(0) # Ensure we upload the whole file
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <File: gigi>
> seek = property(lambda self: self.file.seek)
E AttributeError: 'bytes' object has no attribute 'seek'
../../.local/share/virtualenvs/--M2Y9QA9/lib/python3.7/site-packages/django/core/files/utils.py:20: AttributeError
问题似乎出在这一行:
name='gigi',
> file=thumb_file
)
将 thumb_file 传递给文件字段的正确方法是什么?
先尝试创建模型实例,然后在FileField
上单独调用save()
,将BytesIO
实例包装在Django的File
中,例如:
from django.core.files import File
mymodel = MyModelTosave()
mymodel.file.save('gigi', File(thumb_io), True)
请注意,第三个参数 True
确保在文件保存完成后保存模型实例本身。
在创建 Django 文件之前添加这个就可以了:
thumb_io.seek(0)
我正在尝试在模型中附加一个当前不在磁盘上但存在于内存中的文件。 该模型使用 django-filer,如果我传递一个常规文件它可以工作,使用 io 流它会失败。
def test_save_bytesio():
from PIL import Image
import io
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.core.files.uploadedfile import SimpleUploadedFile
img = Image.new("RGB",(4,4))
thumb_io = io.BytesIO()
img.save(thumb_io, format='JPEG')
# thumb_file = InMemoryUploadedFile(thumb_io, None, '{}_da_ordine.jpg'.format(1), 'image/jpeg',
# thumb_io,None)
thumb_file = DjangoFile(thumb_io.getvalue(), name='gigi')
#thumb_file = DjangoFile(thumb_io, name='gigi')
mymodel = MyModelTosave.objects.create(
name='gigi',
file=thumb_file
)
这是错误堆栈:
return field.pre_save(obj, add=True)
../../.local/share/virtualenvs/--M2Y9QA9/lib/python3.7/site-packages/django/db/models/fields/files.py:288: in pre_save
file.save(file.name, file.file, save=False)
../../.local/share/virtualenvs/--M2Y9QA9/lib/python3.7/site-packages/filer/fields/multistorage_file.py:121: in save
content.seek(0) # Ensure we upload the whole file
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <File: gigi>
> seek = property(lambda self: self.file.seek)
E AttributeError: 'bytes' object has no attribute 'seek'
../../.local/share/virtualenvs/--M2Y9QA9/lib/python3.7/site-packages/django/core/files/utils.py:20: AttributeError
问题似乎出在这一行:
name='gigi',
> file=thumb_file
)
将 thumb_file 传递给文件字段的正确方法是什么?
先尝试创建模型实例,然后在FileField
上单独调用save()
,将BytesIO
实例包装在Django的File
中,例如:
from django.core.files import File
mymodel = MyModelTosave()
mymodel.file.save('gigi', File(thumb_io), True)
请注意,第三个参数 True
确保在文件保存完成后保存模型实例本身。
在创建 Django 文件之前添加这个就可以了:
thumb_io.seek(0)