【问题标题】:How to upload a file/image through tastypie如何通过tastepie上传文件/图像
【发布时间】:2013-08-25 04:31:10
【问题描述】:

我在使用美味派开发 api 时遇到问题。 我基本上想知道是否有办法在json中直接将图像发布到tastepie。

在我的模型中,我现在使用的是 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”

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

谢谢

【问题讨论】:

  • 您需要编写一个自定义 hydrate_photo() 函数,该函数接收您的 JSON 子文档并填充 FileField - TastyPie 不支持您尝试执行的开箱即用操作。
  • 好的,谢谢你的回答

标签: javascript python django file-upload tastypie


【解决方案1】:

【讨论】:

    猜你喜欢
    • 2016-07-11
    • 2013-05-26
    • 2015-09-22
    • 2012-11-24
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多