how to manually assign imagefield in Django
Asked Answered
M

1

20

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...

Maas answered 1/5, 2009 at 12:32 Comment(0)
C
20

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)
Caslon answered 1/5, 2009 at 13:13 Comment(2)
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-closedIfc
model_instance.image_field.save('filename.jpeg', <file>, True) worked for meAlmucantar

© 2022 - 2024 — McMap. All rights reserved.