2011-06-17 56 views
1

我有一個名爲ClassFile的模式,需要的FileField(文件):因爲request.FILES [「文件」]不存在如何修改Django中的對象的FileField?

class ClassFile(models.Model): 

    timestamp = models.DateTimeField(auto_now_add=True, editable=False) 
    document = models.FileField(upload_to="classFiles/%Y/%m/%d/") 
    description = models.TextField('File description', max_length=500) 
    shown = models.BooleanField('Allow Students to view this', default=True) 
    course = models.ForeignKey(Course, related_name='files', editable=False) 
    uploader = models.ForeignKey(User, related_name='class_files_uploaded') 

試圖拯救低於ClassFile的實例文件上傳表單提交失敗後,顯然:

f = ClassFile(
        document = request.FILES['file'], 
        description = form.cleaned_data['description'], 
        shown = form.cleaned_data['shown'], 
        course = course, 
        uploader = request.user, 
       ) 
f.save() 

我在想,如果我保存此對象的正確方法。 request.FILES ['file']是否有正確的類型?謝謝。

回答

0

爲您的文件,關鍵是字段名document(不file),而且最好使用cleaned_data

document = form.cleaned_data['document'] 

form.cleaned_data['document'](或request.FILES['document'])返回一個UploadedFile對象。

+0

謝謝,那有效! – dangerChihuahua007

相關問題