Skip to content

Instantly share code, notes, and snippets.

@shermaza
Created April 30, 2018 04:05
Show Gist options
  • Select an option

  • Save shermaza/2153d06028d91875360ff91bffe8fe38 to your computer and use it in GitHub Desktop.

Select an option

Save shermaza/2153d06028d91875360ff91bffe8fe38 to your computer and use it in GitHub Desktop.
Save an image from a url to a Django model ImageField
import os
import requests
from django.core.files.base import ContentFile
def save_image_field_from_url(model, field, url, filename):
image = requests.get(url)
getattr(model, field).save(
os.path.basename(filename),
ContentFile(image.content)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment