1

我有一个 Django 项目,其中有一个从 DjangoCreateView类继承的视图。此视图用于将文件上传到服务器,并使用UploadedFile我创建的模型。UploadedFile还需要与项目相关联,该项目在模型中存储为调用ForeignKeyprojectUploadedFile

项目 ID 作为 URL 的一部分传入:(r'^projects/(?P<proj_key>\d+)/$', UploadedFileCreateView.as_view(), {}, 'upload-new')

因为project不是真正的表单字段,所以我知道我需要使用ModelForm;来排除它。但是,即使我这样做了,django 也永远不会进入该form_valid方法(如果我在其中进行日志调用,它将永远不会被写入日志,尽管日志记录工作正常)。我猜这ForeignKey是罪魁祸首,因为据我所知,在我添加它之前它是有效的。我不明白为什么 django 即使在我 exclude 之后也不认为该表单有效project

这是我的model定义:

class Project(models.Model):
    """This is a project that is owned by a user and contains many UploadedFiles."""
    name = models.CharField(max_length=200)

class UploadedFile(models.Model):
    """This represents a file that has been uploaded to the server."""
    STATE_UPLOADED = 0
    STATE_ANNOTATED = 1
    STATE_PROCESSING = 2
    STATE_PROCESSED = 4
    STATES = (
        (STATE_UPLOADED, "Uploaded"),
        (STATE_ANNOTATED, "Annotated"),
        (STATE_PROCESSING, "Processing"),
        (STATE_PROCESSED, "Processed"),
    )

    status = models.SmallIntegerField(choices=STATES,
        default=0, blank=True, null=True) 
    file = models.FileField(upload_to=settings.XML_ROOT)
    project = models.ForeignKey(Project)

    def __unicode__(self):
        return self.file.name

    def name(self):
        return os.path.basename(self.file.name)

    def save(self, *args, **kwargs):
        if not self.status:
            self.status = self.STATE_UPLOADED
        super(UploadedFile, self).save(*args, **kwargs)

    def delete(self, *args, **kwargs):
        os.remove(self.file.path)
        self.file.delete(False)
        super(UploadedFile, self).delete(*args, **kwargs)

class UploadedFileForm(forms.ModelForm):
    class Meta:
        model = UploadedFile
        excludes = ('project',)

这是我的视图定义:

class UploadedFileCreateView(CreateView):
    model = UploadedFile

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.project_id = self.kwargs['proj_key']
        self.object.save()
        f = self.request.FILES.get('file')
        data = [{'name': f.name,
            'url': settings.MEDIA_URL + "files/" + f.name.replace(" ", "_"),
            'project': self.object.project.get().pk,
            'delete_url': reverse('fileupload:upload-delete',
                args=[self.object.id]),
            'delete_type': "DELETE"}]
        response = JSONResponse(data, {}, response_mimetype(self.request))
        response['Content-Disposition'] = 'inline; filename=files.json'
        return super(UploadedFileCreateView, self).form_valid(form)

    def get_context_data(self, **kwargs):
        context = super(UploadedFileCreateView, self).get_context_data(**kwargs)
        return context
4

1 回答 1

1

我看到两个可能的问题:

1)表单关键字是exclude,不是excludes

通常,建议是支持明确列出fields要包含在内的内容,这样您就不会意外暴露以后可能添加的任何字段,但exclude会起作用。

2)您实际上并没有在视图中使用您的自定义表单类。将form_class属性设置为UploadedFileForm

于 2013-10-24T03:20:08.027 回答