Created
April 30, 2018 04:05
-
-
Save shermaza/2153d06028d91875360ff91bffe8fe38 to your computer and use it in GitHub Desktop.
Save an image from a url to a Django model ImageField
This file contains hidden or 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 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