Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django tastypie:有关于POST中文件上传的例子吗?_Python_Django_Tastypie - Fatal编程技术网

Python Django tastypie:有关于POST中文件上传的例子吗?

Python Django tastypie:有关于POST中文件上传的例子吗?,python,django,tastypie,Python,Django,Tastypie,有没有人能给出一个完整的使用tastypie文件字段的例子,包括服务器端和客户端 以下是我尝试过的: #models.py class Foo(models.Model): img = models.ImageField(upload_to="images", null=True, blank=True) body = models.CharField() #api.py class FooResource(ModelResource): img = fields.Fi

有没有人能给出一个完整的使用tastypie文件字段的例子,包括服务器端和客户端

以下是我尝试过的:

#models.py
class Foo(models.Model):
    img = models.ImageField(upload_to="images", null=True, blank=True)
    body = models.CharField()

#api.py
class FooResource(ModelResource):
    img = fields.FileField(attribute="image", null=True, blank=True)
    class Meta:
        queryset = Foo.objects.all()
如果我尝试使用curl创建foo对象,例如

>>> curl -F "body=test" -F "img=@local_img.png" http://localhost:8000/api/0.1/foo/
foo对象已成功创建,但
img
字段为空。我可以在调试器中看到,在保存bundle对象时,确实有一个img字段,其中包含一个
InMemoryUploadedFile
对象,因此请求可能是正常的。
我哪里做错了?非常欢迎使用代码片段,谢谢

您的资源应该如下所示:

class FooResource(ModelResource):
    img = fields.FileField(attribute="img", null=True, blank=True)
    class Meta:
        queryset = Foo.objects.all()
属性应与模型中的字段相对应。
如文件所述:

ApiField.属性

命名由资源包装的对象的实例属性的字符串


当我这样做时,我得到一个错误:
指示的格式“multipart/form data”没有可用的反序列化方法。
我缺少一些简单的东西吗?我找到了一个解决缺少反序列化错误的方法,基于。