MongoDB
 sql >> Base de données >  >> NoSQL >> MongoDB

Tastypie renvoie les données des requêtes DELETE ?

En regardant la source et documentation pour savoureuxpie, il semble que vous devrez remplacer deux fonctions de ModelResource (qui MongoEngineResource hérite ):

  1. obj_delete qui supprime l'objet.

  2. delete-detail qui est appelé sur une requête DELETE et appelle obj_delete renvoie alors un 204 No Content ou 404 Not Found

Je n'ai pas travaillé avec savoureuxpie donc tout cela vient de regarder la documentation, mais c'est au moins un point de départ. Vous devrez faire quelque chose comme ceci pour votre classe :

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