django-rest-framework limit the allowed_methods to GET

后端 未结 4 1416
名媛妹妹
名媛妹妹 2020-12-31 11:04

I Have just started with django-rest-framework. Pretty enthousiastic about it, except for the fact there are very little examples available. getting the api working is going

4条回答
  •  臣服心动
    2020-12-31 11:20

    Sorry for necro, but I stumbled upon this question looking for a similar issue.

    I only wanted to allow retrieve() but not to list(). What I ended up doing:

    from rest_framework import viewsets
    from rest_framework.exceptions import MethodNotAllowed
    
    from myapp.models import MyModel
    
    class MyViewSet(viewsets.ModelViewSet):
        http_method_names = ["get"]
        queryset = MyModel.objects.all()
        serializer_class = MySerializer
    
        def list(self, request, *args, **kwargs):
            raise MethodNotAllowed("GET")
    

提交回复
热议问题