0

I am trying to read image from url and save into db.

image = Image()
name = urlparse(imgurl).path.split('/')[-1]
image.bild.save(name, File(urllib2.urlopen(imgurl).read()), save=False)#error line
image.von_location = location
image.save()

this is my Image model

class Image(models.Model):
   von_location= models.ForeignKey(Location,related_name="locations_image",default=0)
   bild = models.ImageField(upload_to=locationimage,default='')
   def __unicode__(self):
       return self.bild.name

the error below is coming when i try to call the save() method of image file.

AttributeError: str has no attribute name

name is just a name of image as i read this here https://docs.djangoproject.com/en/dev/ref/files/file/

this is the screenshot of error message

enter image description here

3
  • what does return self.bild return ? Commented Oct 14, 2013 at 20:20
  • @karthikr, self.bild.name returns the name of image file, this is what i thought Commented Oct 14, 2013 at 20:24
  • 1
    Can you try image.bild.save(name, File(urllib2.urlopen(imgurl)), save=False)? urllib2.urlopen is supposed to return a file-like object already. Also, the stack trace seems to have different variable names than the listed code Commented Oct 14, 2013 at 21:32

1 Answer 1

3

I was able to reproduce the error on Django 1.4 with the test app below. Basically, you'll need to use a ContentFile instead of a File since you are reading the contents of the image. If you try to pass the file object directly to File, you'll run into an unknown size error.

https://docs.djangoproject.com/en/1.4/ref/models/fields/#django.db.models.FieldFile.save

Basic test app:

models.py

class TestModel(models.Model):
    file = models.FileField(upload_to="test")

    def __unicode__(self):
        return self.file.name

tests.py

import os.path
import urllib2
from urlparse import urlparse

from django.test import TestCase
from django.core.files import File
from django.core.files.base import ContentFile

from testapp.models import TestModel

class SimpleTest(TestCase):
    def test_models(self):
        test_model = TestModel()
        imgurl = 'http://www.stackoverflow.com/favicon.ico'
        name = urlparse(imgurl).path.split('/')[-1]
        content =  urllib2.urlopen(imgurl).read()
        #test_model.file.save(name, File(content), save=False) # error line
        test_model.file.save(name, ContentFile(content), save=False)
        test_model.save()
        print test_model
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, but ContentFile takes strings only i thought?
Images are stored correctly in the test. I believe based on the headers present in the request for urlopen it'll try to decode it appropriately. In this case for an image, it'll be a byte string, which then is handled appropriately by ContentFile as it uses a StringIO to rebuild the file object from a byte string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.