请原谅我英语不好;-)想象一下这个非常简单的模型:class Photo(models.Model): image = models.ImageField('Label', upload_to='path/')我想从图像URL创建照片(即,不是在django管理站点中手动创建)。我认为我需要做这样的事情:from myapp.models import Photoimport urllibimg_url = 'http://www.site.com/image.jpg'img = urllib.urlopen(img_url)# Here I need to retrieve the image (as the same way that if I put it in an input from admin site)photo = Photo.objects.create(image=image)如果不告诉我,我希望我已经很好地解释了问题。谢谢 :)编辑:这可能有效,但我不知道如何转换content为Django文件:from urlparse import urlparseimport urllib2from django.core.files import Filephoto = Photo()img_url = 'http://i.ytimg.com/vi/GPpN5YUNDeI/default.jpg'name = urlparse(img_url).path.split('/')[-1]content = urllib2.urlopen(img_url).read()# problem: content must be an instance of Filephoto.image.save(name, content, save=True)
添加回答
举报
0/150
提交
取消