gpt4 book ai didi

python - 一个类似 StringIO 的类,它扩展了 django.core.files.File

转载 作者:太空狗 更新时间:2023-10-29 22:00:11 26 4
gpt4 key购买 nike

class MyModel(models.Model)
image = models.FileField(upload_to="blagh blagh...")
#more spam...

我在内存中有一个文件,我想通过 Django FileField 保存方法保存它,如下所示:

photo.image.save(name, buffer) # second arg should be django File

我试过使用 StringIO,但它没有扩展 django.core.files.File,因此没有实现方法 chunks()。我将它包装在一个 File 对象中:

buffile = File(buffer, name) # first argument should be a file
photo.image.save(name, buffile)

但是 File 方法使用提供的文件的大小和名称字段。 StringIO 没有定义它们。我找到了 this , 但链接已失效

最佳答案

您可以使用 ContentFile 而不是 File

from django.core.files.base import ContentFile

photo.image.save(name, ContentFile(buffer))

关于python - 一个类似 StringIO 的类,它扩展了 django.core.files.File,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3330677/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com