Pass a custom param in URL with a ModelViewSet

大兔子大兔子 提交于 2021-02-11 12:27:15

问题


What's the best way of passing a param using a ModelViewSet? Forexample achieving something like this :

http://127.0.0.1:8000/api/v1/financing-settings/template/?param=block

Below is the approach I was using but found out I have set the param in the body section, but it's not what I want :

class TemplateView(ModelViewSet):
    """ViewSet for Saving Block/ Step template."""

   
    def list(self, request, *args, **kwargs):

        """Get list of Block/Steps with is_process_template is equal to True."""
        param = request.data['param']

        if param == "block":
            _block = Block.objects.filter(is_process_template=True).values()
            return JsonResponse({"data": list(_block)}, safe=False, status=200)

        elif param == "step":
            _step = Step.objects.filter(is_process_template=True).values()
            return JsonResponse({"data": list(_step)}, safe=False, status=200)

        return Response(status=status.HTTP_204_NO_CONTENT)

回答1:


 param = request.GET.get('param')

or for a post request

 param = request.POST.get('param')


来源:https://stackoverflow.com/questions/65378199/pass-a-custom-param-in-url-with-a-modelviewset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!