2012-01-17 11 views
0

私はdjangoプロジェクトを持っており、xlwt(ファイル生成の最後のスニペット)を使用してExcelファイルを作成しました。Excelファイルを保存して新しいオブジェクトに添付するにはどうすればいいですか?

export_wb.save(output) 
output.seek(0) 
response = HttpResponse(output.getvalue()) 
response['Content-Type'] = 'application/vnd.ms-excel' 
response['Content-Disposition'] = 'attachment; filename='+filename 
return response 

は今、私の見解で、私はこのファイルを生成して、新しいオブジェクトにアタッチし、それを保存したいので、私は添付のExcelファイルで管理して新しいオブジェクトを持っています。 (ファイルは、ファイルアップロードフィールドです)'HttpResponse' object has no attribute '_committed'

は、私が「ファイル」プロパティに設定していたオブジェクトを好きしていないようです:私は、私も、このエラーを取得しておく。この

def test(request): 
    exported_ingredients = export(request, app_name='ingredients', model_name='ingredient') 
    new_export = IngredientExportItem(file_name="x", slug="x", file=exported_ingredients) 
    new_export.save() 
    return HttpResponseRedirect('/') 

ような何かをしようとしています。私がオブジェクトを返すだけで、ブラウザはファイルを正しくダウンロードして、ファイルはOKです。

+1

完全なトレースバックを投稿できますか? – plaes

+0

ここに完全なトレースバックがありますhttp://dpaste.com/689377/ – darren

答えて

2

あなたの応答はdjangoファイルオブジェクトではなく、django HttpResponseオブジェクトです。

文字列からdjangoファイルオブジェクトを作成する場合は、ContentFileをチェックしてください。

from django.core.files.base import ContentFile 

def test(request): 
    http_response = export(request, app_name='ingredients', model_name='ingredient') 
    file_ = ContentFile(http_response.content) 
    file_.name = http_response['Content-Disposition'].split('=')[-1] 

    new_export = IngredientExportItem(file_name="x", slug="x", file=file_) 
    new_export.save() 
    return HttpResponseRedirect('/') 
+0

ありがとう!オブジェクトが正しくダウンロードされていたので混乱しましたが、今は保存するために正しいタイプに変換する必要があります。 – darren

+0

@mongoose_za、NP - それは 'Content-Disposition'と他のヘッダに基づいたダウンロードであると解釈するブラウザです。 –