使用rarfile和django提取图像

时间:2015-09-05 08:57:07

标签: python django python-2.7

我正在尝试从存档中提取图像并在django模型中保存图像

    with rarfile.RarFile(archive) as rf:
        for name in rf.namelist():
           image = rf.open(name)
               fimg = FarcopImages.objects.create(scheme=image, article=article)
               fimg.save()

错误:

'PipeReader' object has no attribute '_committed'

1 个答案:

答案 0 :(得分:1)

您无法将RarFile - esque文件实例保存到模型中。您需要使用内置的django File对象。

https://docs.djangoproject.com/en/1.8/topics/files/#the-file-object

# Something like...
from django.core.files import File

# ...

with rf.open(name) as file_like_object:
    FarcopImages.objects.create(scheme=File(file_like_object), article=article)