Django: ajouter une image dans un ImageField à partir de l'url de l'image

veuillez m'excuser pour mon laid anglais 😉

Imagine ce modèle très simple :

class Photo(models.Model):
    image = models.ImageField('Label', upload_to='path/')

Je voudrais créer une Photo à partir d'une URL de l'image (c'est à dire, pas à la main dans le site d'administration de django).

Je pense que j'ai besoin de faire quelque chose comme ceci :

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)

J'espère que j'ai bien expliqué le problème, si pas de me le dire.

Merci 🙂

Edit :

Cela peut fonctionner, mais je ne sais pas comment convertir content à un django Fichier :

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)
InformationsquelleAutor | 2009-09-08