python - 如何限制django rest框架中的 Action

标签 python django rest django-rest-framework swagger

我正在使用具有多个操作(检索、列表、创建等...)的 View 集。我还使用 swagger 来清晰地了解我的 API。问题是它充满了未使用的方法(PATCH、PUT、DELETE)并且它弄乱了 View 。

我已尝试在我的 View 集中执行此操作:allowed_methods = ('GET','POST',)

swagger 仍然有所有这些未使用的方法。我怎样才能改变这种行为?还有另一种方法可以限制 View 集中的操作数量吗?或者问题出在自大方面?

最佳答案

您需要更准确地组合您的 View 集 View 以摆脱它们。

默认的 ModelViewSet 是:

class ModelViewSet(
        mixins.CreateModelMixin,
        mixins.RetrieveModelMixin,
        mixins.UpdateModelMixin,
        mixins.DestroyModelMixin,
        mixins.ListModelMixin,
        GenericViewSet):
    pass

因此,如果您只想说出列表和创建方法,它将是:

class MyViewSet(
        mixins.CreateModelMixin,
        mixins.ListModelMixin,
        GenericViewSet):
    serializer_class = ....
    queryset = ....

关于python - 如何限制django rest框架中的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33608847/

相关文章:

Python SQLAlchemy : Data source name not found and no default driver specified

python - Django_Hosts 导入错误

java - 将值添加到 Junit 中的 multiPart(使用 Jersey )

python - 如何从字节串中恢复二维 numpy.array?

python - 为 zipfile 定义的 __enter__ 和 __exit__ 在哪里?

python - 单个 Django 模型,多个表?

python - 使用 djangorestframework 0.3.0 和 django 1.7

web-services - 如果 REST 是无状态的,那么它如何处理来自客户端的多个请求?

java - Camel - REST DSL (2.14.0) 和 String bean

Python:检查互联网连接(多次)