Tastypie从DELETE请求中返回数据吗?

Bri*_*ice 1 django mongodb tastypie

我有一个简单的资源,我想执行DELETE。成功后,我想获取已删除对象的ID。根据文档,always_return_data- 指定所有HTTP方法(DELETE除外)应返回数据的序列化形式。

http://django-tastypie.readthedocs.org/en/latest/resources.html#always-return-data

class SimpleResource(resources.MongoEngineResource):
    class Meta:
        queryset = Simple.objects.all()
        resource_name = 'simple'
        allowed_methods = ('get', 'put', 'post', 'delete', 'patch')
        always_return_data = True
Run Code Online (Sandbox Code Playgroud)

问题: 是否有序列化数据以返回已删除的对象?

pco*_*nel 5

纵观文档的tastypie,它看起来像你需要重写两种功能ModelResource(它MongoEngineResource继承):

  1. obj_delete 删除对象。

  2. delete-detail 在DELETE请求上调用该函数,obj_delete然后调用返回204 No Content404 Not Found

我还没有和styrepie一起工作,所以这全都来自看文档,但这至少是一个起点。您需要在课堂上做类似的事情:

class SimpleResource(resources.MongoEngineResource):
    class Meta:
        queryset = Simple.objects.all()
        resource_name = 'simple'
        allowed_methods = ('get', 'put', 'post', 'delete', 'patch')
        always_return_data = True


    def obj_delete(self, bundle, **kwargs):
        try:
            # get an instance of the bundle.obj that will be deleted
            deleted_obj = self.obj_get(bundle=bundle, **kwargs)
        except ObjectDoesNotExist:
            raise NotFound("A model instance matching the provided arguments could not be found.")
        # call the delete, deleting the obj from the database
        super(MongoEngineResource, self).obj_delete(bundle, **kwargs)
        return deleted_obj


    def delete_detail(self, request, **kwargs):
        bundle = Bundle(request=request)

        try:
            # call our obj_delete, storing the deleted_obj we returned
            deleted_obj = self.obj_delete(bundle=bundle, **self.remove_api_resource_names(kwargs))
            # build a new bundle with the deleted obj and return it in a response
            deleted_bundle = self.build_bundle(obj=deleted_obj, request=request)
            deleted_bundle = self.full_dehydrate(deleted_bundle)
            deleted_bundle = self.alter_detail_data_to_serialize(request, deleted_bundle)
            return self.create_response(request, deleted_bundle, response_class=http.HttpNoContent)
        except NotFound:
            return http.HttpNotFound()
Run Code Online (Sandbox Code Playgroud)