zoukankan      html  css  js  c++  java
  • 将非drf接口配置到swagger

      近来在编写接口的过程中,发现非正常的drf接口,即使框架配置了swagger,也不展示;将发现的问题及解决方案记录,以免再次踩坑;

    问题表现:

    def export_excel(request):
        pass

    解决方案:增加@api_view()装饰器

    方式1:直接通过注释进行描述

    from rest_framework.decorators import api_view
    
    
    @api_view(["POST"])
    def export_excel(request):
    """
    post:
    导出查询数据;

    请求params:?file_url= ,不传时,导出到桌面
    请求体:{"data": 数据库执行接口返回的data或errors中的内容}
    """
      pass

    表现:

     方式2:使用@swagger_auto_schema进行设置

    from rest_framework.decorators import api_view
    from drf_yasg import openapi
    
    
    re_params = openapi.Parameter('file_url', openapi.IN_QUERY, description="请求参数:导出路径,非必填,默认桌面", type=openapi.TYPE_STRING)
    # 导出查询数据
    @swagger_auto_schema(
        method='post',
        operation_summary='导出查询数据',
        manual_parameters=[re_params],
        request_body=openapi.Schema(
            type=openapi.TYPE_OBJECT,
            properties={
                "data": openapi.Schema(title="此请求体用查询的响应data,格式为{'data': data}", type=openapi.TYPE_STRING),
            }
        ),
        responses={200: "ok"}
    )
    @api_view(["POST"])
    def export_excel(request):
      pass

    表现:

    yasg文档:https://drf-yasg.readthedocs.io/en/stable/

  • 相关阅读:
    python 线程之 数据同步 Queue
    python 线程之threading(五)
    python 线程之 threading(四)
    python 线程之 threading(三)
    php-属性和方法的重载
    wordpress-4.7.2-zh_CN页面加载慢
    php-__autoload()
    php-_toString()方法
    php-final
    php-parent::和self::
  • 原文地址:https://www.cnblogs.com/guo126/p/15507239.html
Copyright © 2011-2022 走看看