Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.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
Django Tastypie反序列化多部分/表单数据以上载文件_Django_Tastypie - Fatal编程技术网

Django Tastypie反序列化多部分/表单数据以上载文件

Django Tastypie反序列化多部分/表单数据以上载文件,django,tastypie,Django,Tastypie,我试图通过多部分/表单数据表单和Tastypie API上传文件,但遇到了一些问题: 我的模型: class Client(models.Model): account = models.ForeignKey(Account) client_image = models.FileField(upload_to=client_image_path, default="/assets/img/default-user-image.png", blank=True, null=True

我试图通过多部分/表单数据表单和Tastypie API上传文件,但遇到了一些问题:

我的模型:

class Client(models.Model):
    account = models.ForeignKey(Account)
    client_image = models.FileField(upload_to=client_image_path, default="/assets/img/default-user-image.png", blank=True, null=True)
    client_image_thumb = models.FileField(upload_to=client_image_thumb_path, default="/assets/img/default-user-image.png", blank=True, null=True)
我使用的是自定义反序列化方法,如Tastypie问题42中所述:

这是我对应的ModelResource:

class ClientResource(MultipartResource, ModelResource):
    account = fields.ForeignKey(AccountResource, 'account')

    class Meta():
        queryset = Client.objects.all()
        always_return_data = True
        resource_name = 'account/clients/client-info'
        authorization = AccountLevelAuthorization()
        list_allowed_methods = ['get','post','put','delete','patch']
        detail_allowed_methods = ['get', 'post', 'put', 'delete','patch']
        authentication = ApiKeyAuthentication()
        filtering = {
            'username': ALL,
        }
如果我使用内容类型application/JSON发表文章,但不包括client_image字段,它将成功创建一个新的客户机对象。这表明模型/资源正在正常工作

但是,当我尝试使用多部分/表单数据内容类型时,我可以看到它通过我的反序列化程序适当地使用以下负载:

------WebKitFormBoundaryp0Q7Q9djlsvVGwbb
Content-Disposition: form-data; name="{%0D%0A%22account%22"

"/api/v1/account/account-info/21/",

------WebKitFormBoundaryp0Q7Q9djlsvVGwbb
Content-Disposition: form-data; name="client_image"; filename="donavan.jpg"
Content-Type: image/jpeg

------WebKitFormBoundaryp0Q7Q9djlsvVGwbb--
我在调试时也看到了这个QueryDict,它正确地显示了InMemoryUploadedFile:

<QueryDict: {u'client_image': [<InMemoryUploadedFile: donavan.jpg (image/jpeg)>], u'{%0D%0A%22account%22': [u'"/api/v1/account/account-info/21/"']}>

但我一直在犯这样的错误:

{错误消息:''回溯:''回溯(最近一次呼叫最后一次): 文件 “/Users/stevewirig/Documents/www/vu/venv/lib/python2.7/site packages/tastype/resources.py”, 第202行,包装器响应=回调(请求,*args,**kwargs) 文件 “/Users/stevewirig/Documents/www/vu/venv/lib/python2.7/site packages/tastype/resources.py”, 第440行,在调度列表中返回自调度(“列表”,请求, **kwargs)文件“/Users/stevewirig/Documents/www/vu/venv/lib/python2.7/site packages/tastypi/resources.py”, 第472行,调度响应=方法(请求,**kwargs)文件 “/Users/stevewirig/Documents/www/vu/venv/lib/python2.7/site packages/tastype/resources.py”, 第1328行,在post_列表中更新的_bundle=self.obj_create(bundle, **self.remove_api_resource_names(kwargs))文件“/Users/stevewirig/Documents/www/vu/venv/lib/python2.7/site packages/tastypi/resources.py”, 第2104行,在obj_create bundle=self.full_(bundle)文件中 “/Users/stevewirig/Documents/www/vu/venv/lib/python2.7/site packages/tastype/resources.py”, 第890行,在full\u hydrate value=field\u object.hydrate(bundle)文件中 “/Users/stevewirig/Documents/www/vu/venv/lib/python2.7/site packages/tastype/fields.py”, 第732行,水合物值=super(ToOneField,self)。水合物(束) 文件 “/Users/stevewirig/Documents/www/vu/venv/lib/python2.7/site packages/tastype/fields.py”, 第165行,在elif self.attribute和getattr(bundle.obj)中, self.attribute,无):文件 “/Users/stevewirig/Documents/www/vu/venv/lib/python2.7/site packages/django/db/models/fields/related.py”, 第343行,在获取提升self.field.rel.to.DoesNotExist DoesNotExist中 “}


有什么想法可以打破这个吗?提前谢谢

当我发布数据时,没有提供必要的字段,这种情况就发生了。过帐时必须提供不能为空的字段

按如下方式初始化序列化程序:

serializer=serializer(格式=['json'])

<QueryDict: {u'client_image': [<InMemoryUploadedFile: donavan.jpg (image/jpeg)>], u'{%0D%0A%22account%22': [u'"/api/v1/account/account-info/21/"']}>