0

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)
4
  • Why is your approach not what you want? The param is not in the request body it is in request.GET, DRF adds the request.data dict which is a merge of all GET/POST/FILES data from the request afaik Commented Dec 20, 2020 at 8:43
  • @IainShelvington, my apporoach is not what I want leading to a challenge am facing now, though please shed more light on what you've said Commented Dec 20, 2020 at 8:49
  • What challenge? Reading the param from request.GET is pretty much the way to read query parameters Commented Dec 20, 2020 at 8:51
  • @IainShelvington, I have seen it it, it returns a QueryDict: Commented Dec 20, 2020 at 8:54

2 Answers 2

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

or for a post request

 param = request.POST.get('param')
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.