Tastypie从DELETE请求返回数据?

时间:2014-05-05 21:25:20

标签: 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

问题: 无论如何序列化数据以返回已删除的对象?

2 个答案:

答案 0 :(得分:4)

查看tastypie的sourcedocumentation,看起来你需要覆盖ModelResource的两个函数MongoEngineResource inherits }):

  1. obj_delete删除对象。

  2. delete-detail在DELETE请求中调用并调用obj_delete然后返回204 No Content404 Not Found

  3. 我没有使用tastypie,所以这一切都来自查看文档,但它至少是一个起点。你需要对你的班级做这样的事情:

    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()
    

答案 1 :(得分:0)

您不需要返回已删除的对象。您可以只更新obj_delete中的bundle.obj,然后将其返回到delete_detail。 我正在使用自定义资源和中间件来运行api,但是如果您跳过元模型,那将非常简单。

def obj_delete(self,bundle, **kwargs):
    self.load_user_obj(bundle.request)
    l_instanceId = kwargs['pk']
    try:
        #load the instance
        self.m_objMetaModel.load(l_instanceId,True)

        #do the manipulation on it
        self.m_objMetaModel.REST_IMPL.delete()

        #get an instance back. In my case in the delete I
        #am creating a virtual instance- just an object that 
        #fits the other parts of the api
        bundle.obj = self.m_objMetaModel.instance

        #load the bundle manually
        self.m_objMetaModel.REST_IMPL.reload_data(bundle.obj,bundle.data)

        return bundle
    except Exception as e:
        print(e)
def delete_detail(self, request, **kwargs):
    bundle = Bundle(request=request)

    try:
        # perform the operation returning the recreated bundle
        deleted_bundle = self.obj_delete(bundle=bundle,
                                **self.remove_api_resource_names(kwargs))

        # keep the meta configuration, just to avoid surprises later
        if not self._meta.always_return_data:
            return http.HttpNoContent()
        else:
            #update the bundle, you can skip on the dehydrate if you 
            #are loading the bundle data manually
            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)
    except NotFound:
            return http.HttpNotFound()