【问题标题】:DRF 3.6: How to document input parameters in APIView (for automatic doc generation)?DRF 3.6:如何在 APIView 中记录输入参数(用于自动生成文档)?
【发布时间】:2017-05-03 22:30:11
【问题描述】:

我正在努力使用DRF 3.6 auto-generated interactive documentation 提供输入参数来填写交互模式。

因此,我的 POST 请求得到一个空窗口(实际上需要 3 个参数):

使用 Swagger,我可以使用一些 YAML 直接在文档字符串中执行此操作。 现在,在浏览了 DRF 文档后,我找不到这样做的方法。

class ActivateCustomerView(APIView):

    permission_classes = (AllowAny,)

    def post(self, request):
        """ View dedicated to activating a pre-recorded customer 
            # Should I add some parameters here?
        """

        serializer = ActivateCustomerSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        # ...

【问题讨论】:

    标签: django django-rest-framework


    【解决方案1】:

    我从汤姆克里斯蒂那里得到了答案:

    serializer_class 本身是不够的 - 视图需要实现 get_serializer,请参阅:https://github.com/encode/django-rest-framework/blob/master/rest_framework/schemas.py#L570

    所以在我的情况下,添加这个效果很好:

    def get_serializer(self):
        return ActivateCustomerSerializer()
    

    【讨论】:

      【解决方案2】:

      编辑:我忘了回答有关输入参数的问题。我相信这将基于序列化程序。您是否尝试过指定您的serializer_class

      使用 DRF 的内置文档生成器,您希望将文档字符串放在类级别并包含请求方法:

      class ActivateCustomerView(APIView):
          """
          post:
          View dedicated to activating a pre-recorded customer 
          # Should I add some parameters here?
      
          # if you have a get request
          get:
          # your docs for the get handler
          """
      
          permission_classes = (AllowAny,)
      
          def post(self, request):
      
              serializer = ActivateCustomerSerializer(data=request.data)
              serializer.is_valid(raise_exception=True)
              # ...
      

      【讨论】:

      • 似乎serializer_class 属性仅适用于通用 api 视图。另外,我根本无法使用序列化程序,无论如何都需要设置输入参数。
      • 您使用APIView 而不是GenericAPIView 有什么原因吗?另外,如果我对您的观点的目的的理解是正确的,也许您可​​能想看看UpdateAPIView 并覆盖patch 方法
      • 这里没有 APIView 是故意的。
      • 不,不幸的是它不起作用。但这是意料之中的,因为解决方案与 swagger 有关。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-12
      • 2016-06-14
      • 1970-01-01
      相关资源
      最近更新 更多