請?jiān)徫矣⒄Z不好;-)想象一下這個(gè)非常簡單的模型:class Photo(models.Model): image = models.ImageField('Label', upload_to='path/')我想從圖像URL創(chuàng)建照片(即,不是在django管理站點(diǎn)中手動(dòng)創(chuàng)建)。我認(rèn)為我需要做這樣的事情: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)如果不告訴我,我希望我已經(jīng)很好地解釋了問題。謝謝 :)編輯:這可能有效,但我不知道如何轉(zhuǎn)換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)
添加回答
舉報(bào)
0/150
提交
取消