Skip to content

Instantly share code, notes, and snippets.

@iambibhas
Created February 27, 2013 21:30
Show Gist options
  • Save iambibhas/5051911 to your computer and use it in GitHub Desktop.
Save iambibhas/5051911 to your computer and use it in GitHub Desktop.
Programatically save ImageField in Django
import requests
from django.core.files import File
from django.core.files.temp import NamedTemporaryFile
def save_image_from_url(model, url):
r = requests.get(url)
img_temp = NamedTemporaryFile(delete=True)
img_temp.write(r.content)
img_temp.flush()
model.image.save("image.jpg", File(img_temp), save=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment