Django: agregar imagen en un ImageField desde la url de la imagen
por favor disculpame por mi feo ingles ;-)
Imagina este modelo tan simple:
class Photo(models.Model):
image = models.ImageField('Label', upload_to='path/')
Me gustaría crear una foto desde una URL de imagen (es decir, no a mano en el sitio de administración de django).
Creo que necesito hacer algo como esto:
from myapp.models import Photo
import urllib
img_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)
Espero haber explicado bien el problema, si no me lo dicen.
Gracias :)
Editar:
Esto puede funcionar pero no sé cómo convertircontent
a un archivo django:
from urlparse import urlparse
import urllib2
from django.core.files import File
photo = 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 File
photo.image.save(name, content, save=True)