django-tastypie PATCH方法返回400(错误请求)

时间:2014-01-28 17:06:40

标签: python django tastypie

我正在为我的tastypie驱动的api进行测试。以下是使用PATCH方法测试部分更新的一些代码:

response = self.api_client.patch('/api/v1/process/1/', data={
    'code': u'KO-54321'
})
self.assertEqual(response.status_code, 202)

不幸的是它失败了,因为tasypie返回400而不是202.这就是我在回复内容中看到的:

{
  "error": ""
}

我认为水合物/脱水循环存在一些问题,但我无法弄清楚到底在哪里。这是我试图用自定义水合物/脱水方法实现的资源原型。

class ProcessResource(ModelResource):
    type = fields.CharField()
    state = fields.CharField()
    warehouse = fields.CharField()
    customer = fields.CharField()
    provider = fields.CharField()

    class Meta:
        queryset = Process.objects.all()
        include_resource_uri = False
        serializer = PrettySerializer()
        authorization = Authorization()
        authentication = Authentication()
        allowed_methods = ['get', 'post', 'put', 'patch']
        fields = ['id', 'code']
        field_defaults = {
            'required':     True,
            'readonly':     False,
            'hidden':       False,
            'autocomplete': True
        }
        field_configs = {
            ...
        }


    def build_schema(self):
        schema = super(ProcessResource, self).build_schema()
        fields = schema['fields']

        for name, config in fields.items():
            field = self._meta.field_defaults.copy()
            field['type'] = config['type']
            field.update(self._meta.field_configs.get(name, {}))
            fields[name] = field

        return schema


    def dehydrate(self, bundle):
        bundle.data.update({
            'type': dict(Process.TYPES)[bundle.obj.type],
            'state': dict(Process.STATES[bundle.obj.type])[bundle.obj.state],
            'warehouse': bundle.obj.warehouse.name,
            'customer': bundle.obj.contract.customer.name,
            'provider': bundle.obj.contract.provider.name
        })

        return bundle

    def hydrate(self, bundle):
        obj = bundle.obj
        data = bundle.data

        obj.type = dict([(b,a) for (a,b) in Process.TYPES])[data['type']]
        obj.state = dict([(b,a) for (a,b) in Process.STATES[obj.type]])[data['state']]
        obj.warehouse = Warehouse.objects.get(name=data['warehouse'])
        obj.contract = Contract.objects.get(
            provider__name = data['provider'],
            customer__name = data['customer'])

        return bundle

我做错了吗?

0 个答案:

没有答案