问题
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