我在使用 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?
谢谢
最佳答案
是的,您可以只使用 Base64FileField:https://gist.github.com/jstarcher/ef8d91b8e8d058178f20
阅读更多相关信息:https://github.com/toastdriven/django-tastypie/issues/42
关于javascript - 如何通过 tastypie 上传文件/图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18380522/