django-rest-framework limit the allowed_methods to GET

后端 未结 4 1368
名媛妹妹
名媛妹妹 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:37

    If you are using ModelViewSet and still want to restrict some methods you can add http_method_names.

    Example:

    class SomeModelViewSet(viewsets.ModelViewSet):
        queryset = SomeModel.objects.all()
        serializer_class = SomeModelSerializer
        http_method_names = ['get', 'post', 'head']
    

    Once you do this, get, post and head will be allowed. But put, patch and delete will not be allowed.

提交回复
热议问题