15๐
โ
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())
๐คTimmy O'Mahony
16๐
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.
๐คx-yuri
- [Django]-Django REST framework serializer without a model
- [Django]-Why would a django test fail only when the full test suite is run?
- [Django]-Can't install psycopg2 with pip in virtualenv on Mac OS X 10.7
2๐
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)
- [Django]-Django, creating a custom 500/404 error page
- [Django]-Django models, custom functions
- [Django]-Override django's model delete method for bulk deletion
Source:stackexchange.com