문제

I only want data to be sent using POST to this api endpoint. Is there a way to set which request methods are allowed?

class FooViewSet(viewsets.ModelViewSet):
    queryset = Foo.objects.all()
    serializer_class = FooSerializer
도움이 되었습니까?

해결책

Do you need the whole ViewSet or will a normal GenericView be sufficient? You could use the CreateAPIView.

Example:

class FooCreate(generics.CreateAPIView):
    Model = Foo
    serializer_class = FooSerializer

EDIT:

If you do need to use a ViewSet, you could create a custom Router that will only handle post requests. Documentation here, including a read-only (i.e. get) example.

다른 팁

There is probably a better solution, but what I did normally is

if request.method != 'POST':
       return Http404

but this seems to have the answer you need: https://docs.djangoproject.com/en/dev/topics/http/decorators/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top