gpt4 book ai didi

python - 将 StringIO 对象转换为 Django ImageFile

转载 作者:太空狗 更新时间:2023-10-29 21:23:03 25 4
gpt4 key购买 nike

我正在尝试从 StringIO(或 cStringIO,更具体地说)获取数据并将其转换为 django.core.files.images.ImageFile。

但它不起作用。无论如何,我的意思是它在很多方面都失败了,而谷歌让我失望了。

到目前为止我有:

pi = ProductImage(product=product)
image = ImageFile(image_file)
image.name = image_name # defined elsewhere
pi.source_image.save(image_name, image)
pi.save()

我的堆栈跟踪是这样的:

File "dev.py", line 359, in process_csv_item
pi.source_image.save(image_name, image)
File "C:\Python26\lib\site-packages\django\db\models\fields\files.py", line 92, in save
self.name = self.storage.save(name, content)
File "C:\Python26\lib\site-packages\django\core\files\storage.py", line 48, in save
name = self._save(name, content)
File "C:\Python26\lib\site-packages\django\core\files\storage.py", line 168, in _save
for chunk in content.chunks():
File "C:\Python26\lib\site-packages\django\core\files\base.py", line 65, in chunks
counter = self.size
File "C:\Python26\lib\site-packages\django\core\files\base.py", line 39, in _get_size
elif os.path.exists(self.file.name):
AttributeError: 'cStringIO.StringI' object has no attribute 'name'

接下来我可以看哪里?

最佳答案

使用 django.core.files.base.ContentFile(image_file):

pi = ProductImage(product=product)
pi.source_image.save(image_name, ContentFile(image_file.read()))
pi.save()

关于python - 将 StringIO 对象转换为 Django ImageFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5572466/

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