简体   繁体   English

Django Rest:如何使用{pk}禁用POST api

[英]Django Rest: How to disable the POST api with {pk}

How do I get rid of [POST] /api/contact/{pk}/ from the api with Serializer? 如何使用序列化器从api摆脱[POST] /api/contact/{pk}/ (get rid of the second API) My serializer definition is as follows: (摆脱第二个API)我的序列化程序定义如下:

class ContactSerializer(serializers.ModelSerializer):

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

And my ViewSet is as follows: 我的ViewSet如下:

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

Django REST Swagger用户界面

You could use a ReadOnlyModelViewset perhaps? 您可以使用ReadOnlyModelViewset吗? From the docs: 从文档:

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

The ReadOnlyModelViewSet class also inherits from GenericAPIView. ReadOnlyModelViewSet类也继承自GenericAPIView。 As with ModelViewSet it also includes implementations for various actions, but unlike ModelViewSet only provides the 'read-only' actions, .list() and .retrieve(). 与ModelViewSet一样,它也包含各种操作的实现,但与ModelViewSet不同的是,它仅提供“只读”操作,即.list()和.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. 但是,这有点奇怪,因为GenericViewSet不应给您开箱即用的任何操作,因此您应该仅通过ModelViewSet进行POST。

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. GenericViewSet类继承自GenericAPIView,并提供默认的get_object,get_queryset方法和其他通用视图基行为集,但默认情况下不包括任何操作。

This probably means one of those mixins is providing the extra actions for you, see: 这可能意味着这些mixin之一正在为您提供额外的操作,请参阅:

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. 为了使用GenericViewSet类,您将覆盖该类并混入所需的mixin类,或显式定义操作实现。

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. 如果您可以检查那些mixin的定义或在此处发布它们,或者尝试不使用任何mixins的情况下尝试使用ReadOnlyModelViewSet,然后继续进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM