Question

How do I get rid of [POST] /api/contact/{pk}/ from the api with Serializer? (get rid of the second API) My serializer definition is as follows:

class ContactSerializer(serializers.ModelSerializer):

    class Meta:
        model = Contact
        fields = ('chatuser', 'contact', 'is_blocked')

And my ViewSet is as follows:

class ContactViewSet(mixins.CreateModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    queryset = Contact.objects.all()
    serializer_class = ContactSerializer

Django REST Swagger UI

Was it helpful?

Solution

You could use a ReadOnlyModelViewset perhaps? From the docs:

http://www.django-rest-framework.org/api-guide/viewsets#readonlymodelviewset

The ReadOnlyModelViewSet class also inherits from GenericAPIView. As with ModelViewSet it also includes implementations for various actions, but unlike ModelViewSet only provides the 'read-only' actions, .list() and .retrieve().

It's a bit weird though because GenericViewSet shouldn't be giving you any actions out of the box, you should only get POST with ModelViewSet.

The GenericViewSet class inherits from GenericAPIView, and provides the default set of get_object, get_queryset methods and other generic view base behavior, but does not include any actions by default.

This probably means one of those mixins is providing the extra actions for you, see:

In order to use a GenericViewSet class you'll override the class and either mixin the required mixin classes, or define the action implementations explicitly.

If you could check the definitions of those mixins or post them here, alternatively just try using ReadOnlyModelViewSet without any of the mixins and see how you get on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top