使用模型OneToOneField的django-jquery-file-upload

时间:2013-11-18 05:49:42

标签: jquery ajax django file-upload

我正在尝试使用https://github.com/sigurdga/django-jquery-file-upload构建一个小型django图片网站作为蓝图:

我的模型如下:

class Picture(models.Model):

    test = models.SlugField(max_length=50, blank=True)
    slug = models.SlugField(max_length=50, blank=True)
    user = models.OneToOneField(User)
    file = models.ImageField(upload_to="pictures")

def save(self, *args, **kwargs):
    self.slug = self.file.name

    super(Picture, self).save(*args, **kwargs)

class PictureCreateView(CreateView):
    model = Picture
    def form_valid(self, form):
        obj = form.save(commit=False)
        obj.test = self.request.user
        obj.user = self.request.user
        obj.save()
        self.object = obj
        files = [serialize(self.object)]
        data = {'files': files}
        response = JSONResponse(data, mimetype=response_mimetype(self.request))
        response['Content-Disposition'] = 'inline; filename=files.json'
        return response

我有一个ajax按钮,可以像django-jquery-file-upload一样发布图片,但它总是会出现'JSON.parse:意外字符'

奇怪的是,如果我发表评论:

Model
#user = models.OneToOneField(User)
View
#obj.user = self.request.user

该网站将有效。

有人可以对正在发生的事情发表一些看法吗?

杰克

1 个答案:

答案 0 :(得分:0)

我相信错误发生在以下两行:

    files = [serialize(self.object)]

    response = JSONResponse(data, mimetype=response_mimetype(self.request))

您应该在控制台中测试此功能:[serialize(self.object)]