[英]Converting a StringIO object to a Django ImageFile
我正在嘗試從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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.