I have a model that has an ImageField. How can I manually assign an imagefile to it? I want it to treat it like any other uploaded file...
how to manually assign imagefield in Django
Asked Answered
See the django docs for django.core.files.File
Where fd is an open file object:
model_instance.image_field.save('filename.jpeg', fd.read(), True)
If you have to manually temper with files, for example you download an image, this extension may come handy: https://mcmap.net/q/54071/-how-to-upload-a-file-in-django-closed –
Ifc
model_instance.image_field.save('filename.jpeg', <file>, True) worked for me –
Almucantar
© 2022 - 2024 — McMap. All rights reserved.