javascript - 如何通过 tastypie 上传文件/图片

标签 javascript python django file-upload tastypie

我在使用 tastypie 开发 api 时遇到问题。 我基本上想要知道是否有一种方法可以直接在 json 中将图像发布到 tastypie。

在我的模型中,我现在使用的是 ImageField:

  class MyClass(models.Model):
      description = models.TextField()
      user = models.ForeignKey(User)
      photo = models.ImageField(upload_to="image", null=True, blank=True)

然后在我的 api 文件中:

    class InquiryResource(ModelResource):
        user = fields.ForeignKey(UserResource, 'user' ,full=True)
        photo = fields.FileField(attribute = 'photo', null=True, blank = True)

        class Meta :
            queryset = MyClass.objects.all()
            resource_name = "MyClass"
            authorization = Authorization()

当我发送一个只有用户和描述的基本 json 时,它运行良好。 然后当我去添加关于我的图像的信息时:

    { ... ,
    photo : {
       Content-Type : "image/png",
       file : "base64string", <----- this one contains the file as a long string
       name : "test.png"
    } ...}

我收到一条错误消息:“dict”对象没有属性“_commited”

是否有一种“干净的方式”来使用 tastypie 本地上传文件,还是我应该使用 Base64FileField?

谢谢

最佳答案

关于javascript - 如何通过 tastypie 上传文件/图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18380522/

相关文章:

python - 我可以在 Tkinter 应用程序上获取 Youtube 缩略图吗

python - Flask 在导入 pandas(还有 numpy、matplotlib 等)后挂起

python - 无法从在 Docker 容器中运行的 Django 应用程序连接到远程 PostgreSQL 实例

javascript - 在 Chrome 上单击时链接不会获得焦点

javascript - 用JS改变滚动时的颜色效果

Python:前向填充 nans 和 zeros

python - 如何使用 Django 表单或 ModelForm 更新数据库表记录子集中的单个列?

Django 用户模型电子邮件字段 : how to make it mandatory

javascript - Ionic Error Object(...) 不是 NativePageTransitions.fade 中的函数

javascript - Google App Script HTML 表格样式