使用ModelViewSet在URL中传递自定义参数 [英] Pass a custom param in URL with a ModelViewSet

查看:70
本文介绍了使用ModelViewSet在URL中传递自定义参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 ModelViewSet 传递 param 的最佳方法是什么?例如,实现以下目标:

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)

推荐答案

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

或发布请求

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

这篇关于使用ModelViewSet在URL中传递自定义参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆