"Upload" a file from django shell "Upload" a file from django shell django django

"Upload" a file from django shell


Finally I found the answer.

from django.core.files import Filef = File(open(os.path.join(IMPORT_DIR, 'fotos', photo), 'r'))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()


If using Python 3 one adjustment is needed - change 'r' to 'rb':

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