4

我正在使用 Django Rest Framework,目前从我们使用 Get 请求的后端提取一些数据,但由于 URL 限制很高,我们计划实现一个 Post 请求。为此,首先必须使后端 Django Rest API 可用于服务发布请求。

我是 Django 新手,在代码中找不到帖子或获取方法,我只能说我们正在使用视图集,我尝试使用“@detail_route(methods=['post'])”,但这没有不工作,我在这里做错了什么?

class XViewSet(viewsets.ViewSet):
    renderer_classes = ''

    def retrieve(self, request, pk=None):
        try:
            pk = int(pk)
        except ValueError:
            raise InvalidParameterError(parameter_name='id', invalid_value=pk)

        queryset = models.X.objects.all()
        x = get_object_or_404(queryset, pk=pk)

        pipelines = request.query_params.getlist('pipeline[]')

        callsets =

        callset_ids =

        serializer = serializers.XSerializer(x, context={'request': request})

        requested_samples = [z[1:] for z in request.query_params.getlist('s')]

        filtered_calls = []
        serialized_data = serializer.data

        unfiltered_calls = serialized_data.get('calls')
        if unfiltered_calls:
            for serialized_calls in unfiltered_calls:
                if serialized_calls['callset'] in callset_ids:
                    unfiltered_calls = serialized_calls['data']

                    for call in unfiltered_calls:
                        if call['y'] in requested_y:
                            filtered_calls.append(call)

                    break

        serialized_data['calls'] = filtered_calls

        return Response(serialized_data, status=status.HTTP_200_OK)

    def list(self, request):

        qp = self.request.query_params

        validated_qp =

        # generate the query
        query_object =
        query =

        # execute the query
        cursor = connections['default'].cursor()
        cursor.execute(query)

        qs = utils.dictfetchall(cursor)

        # sanitize query results
        if 't' in validated_qp:
            return_data =
        else:
            for x in qs:
                if 'calls' in x:

                    x['calls'] =
                else:
                    x['calls'] = {}

            return_data =

        resp = Response(return_data, status=status.HTTP_200_OK)

        if validated_qp.get(''):
            resp['content-disposition'] =

        return resp
4

1 回答 1

6

您可以使用基于类的视图来处理需求,

from rest_framework.views import APIView


class MyAPI(APIView):
    def get(selfself, request):
        # do stuff with get
        return Response(data="return msg or data")

    def post(self, request):
        post_data = request.data
        # do something with `post_data`
        return Response(data="return msg or data")



更新:使用 ViewSet
ViewSet类为我们提供create()了创建新模型实例的方法。所以我们可以覆盖它来处理进入视图的发布数据。只需create()在您的视图类下添加一个,如下所示

class XViewSet(viewsets.ViewSet):
    renderer_classes = ''

    def create(self, request): # Here is the new update comes <<<<
        post_data = request.data
        # do something with post data
        return Response(data="return data")

    def retrieve(self, request, pk=None):
        # your code

        return Response(serialized_data, status=status.HTTP_200_OK)

    def list(self, request):
        # your code
        return resp
于 2018-02-21T09:08:47.050 回答