[Fixed]-"Upload" a file from django shell

24👍

Finally I found the answer.

from django.core.files import File

f = File(open(os.path.join(IMPORT_DIR, 'fotos', photo), 'rb'))
p = Photo(name=f.name, image=f, parent=supply.supply_ptr)
name = str(uuid1()) + os.path.splitext(f.name)[1]
p.image.save(name, f)
p.save()

7👍

If using Python 3 one adjustment is needed – change ‘r’ to ‘rb’:

f = File(open(os.path.join(IMPORT_DIR, 'fotos', photo), 'rb'))
👤Steve

Leave a comment