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 File

image_model.image_field('path', File().read())

Programmatically saving image to Django ImageField


This works for me on Python3 with Django 2

from urllib.request import urlopen
from urllib.parse import urlparse
from io import BytesIO
from django.core.files.images import ImageFile


from .models import ModelWithImageField

# Solving for SSL Error
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

url = https://www.someurl.com/image.png?other_params_if_they_exist
image_file_name = urlparse(url).path.split('/')[-1]
image_file_content = BytesIO(urlopen(url).read())
ModelWithImageField().image_field.save(image_file_name, image_file_content)

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

from a1.models import Model1                                                               
from django.core.files.images import ImageFile
m = 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.