Created
February 27, 2013 21:30
-
-
Save iambibhas/5051911 to your computer and use it in GitHub Desktop.
Programatically save ImageField in Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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