DRF:GET返回TypeError(500)而不是405

时间:2019-07-09 15:31:23

标签: python django django-rest-framework django-rest-viewsets

我有一个ModelViewSet,它使用@list_route处理到给定端点的POST。

urls.py

router.register(r'my-view-set', MyViewSet, 'my-view-set')

views.py

class MyViewSet(viewsets.ModelViewSet):
    queryset = MyModel.objects.all()
    serializer_class = MyModelSerializer

    @list_route(methods=['post'], url_path='validate')
    def validate(self, request):
        # validate model
        return Response({'success': 'Validated'}, status.HTTP_200_OK)

当通过curl或wget使用HTTP GET访问此终结点时,我得到了正确的405状态代码,如预期的那样:

卷曲:

{"detail":"Method \"GET\" not allowed."}

wget:

2019-07-09 15:06:48 ERROR 405: Method Not Allowed.

但是,如果我使用chrome浏览到端点,则会收到500错误:

TypeError at /myapp/api/v1/my-view-set/validate/
__init__() missing 1 required positional argument: 'instance'

当用GET而不是POST调用此终结点时,如何说服django将405返回浏览器而不是500?

Django == 1.10.5 djangorestframework == 3.5.3

0 个答案:

没有答案