“Upload” a file from django shell

放肆的年华 提交于 2019-11-30 04:58:55

Finally I found the answer.

from django.core.files import File

f = 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'))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!