Django: manually create imagefield in model from existing file on server Django: manually create imagefield in model from existing file on server django django

Django: manually create imagefield in model from existing file on server


I'm marking this as answered, as this is the correct way to do this:

from django.core.files import Fileimage_model.image_field('path', File().read())

Programmatically saving image to Django ImageField


I might be missing something, but this worked for me:

from a1.models import Model1                                                               from django.core.files.images import ImageFilem = Model1.objects.create()                                                                m.f1 = ImageFile(open("1.png", "rb"))      m.save()

for the following model:

class Model1(models.Model):                     f1 = models.ImageField()

This way it didn't work:

m.f1('1.png', File.read(open('1.png', 'r')))

It says:

TypeError: 'ImageFieldFile' object is not callable

Checked with Django 1.7, 1.11.


This works for me on Python3 with Django 2

from urllib.request import urlopenfrom urllib.parse import urlparsefrom io import BytesIOfrom django.core.files.images import ImageFilefrom .models import ModelWithImageField# Solving for SSL Errorimport sslssl._create_default_https_context = ssl._create_unverified_contexturl = https://www.someurl.com/image.png?other_params_if_they_existimage_file_name = urlparse(url).path.split('/')[-1]image_file_content = BytesIO(urlopen(url).read())ModelWithImageField().image_field.save(image_file_name, image_file_content)