2011-04-06 11 views
7

में कनवर्ट करना मैं स्ट्रिंगियो (या cStringIO, अधिक विशेष रूप से) से डेटा लेने की कोशिश कर रहा हूं और इसे django.core.files.images.ImageFile में परिवर्तित कर रहा हूं।एक स्ट्रिंगियो ऑब्जेक्ट को एक Django ImageFile

लेकिन यह काम नहीं करता है। इसके द्वारा, मेरा मतलब है कि यह कई तरीकों से विफल रहता है, और Google ने मुझे असफल कर दिया है।

अब तक मुझे मिल गया है:

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' 

मैं अगले कहाँ देख सकते हैं?

उत्तर

15

उपयोग django.core.files.base.ContentFile (image_file):

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

बंद है, यह किया जा रहा समाप्त हो गया: 'pi.source_image.save (image_name, ContentFile (image_file.read()))' –

+0

मेरे लिए काम नहीं करता है। मॉडल सफलतापूर्वक सहेजा गया है, लेकिन Django को प्रतिपादित करने पर त्रुटि 'IOError: छवि फ़ाइल की पहचान नहीं कर सकता' –

+1

शायद आप प्रारूप प्रकार के लिए पीआईएल में समर्थन खो रहे हैं। यदि आपको अभी भी कोई समस्या है, तो इसके बारे में एक पूर्ण प्रश्न पूछने का प्रयास करें। – gcbirzan

संबंधित मुद्दे