zoukankan      html  css  js  c++  java
  • Django-12-接口文档

    一、coreapi

    1、安装

    pipenv install coreapi

    Pygments、Markdown可选

    2、settings.py中添加

    REST_FRAMEWORK = {
       # 指定用于支持coreapi的Schema
        'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
    }

    3、urls.py中添加

    from rest_framework.documentation import include_docs_urls
    urlpatterns = [
      。。。
        path('docs/', include_docs_urls(title='API接口文档', description='xxx描述'))
    ]

     

    4、给在接口文档中显示接口注释

    在类视图下添加注释即可

    class InterfacesViewSet(viewsets.ModelViewSet):
        """
        GetTester:
        获取tester字段
    
        list:
        获取项目列表数据
    
        retrieve:
        获取项目详情数据
    
        update:
        更新项目数据
    
        create:
        创建项目
    
        partial_update:
        更新一条项目的部分数据
    
        destroy:
        删除一条项目数据
        """
        queryset = Interfaces.objects.all()
        serializer_class = serializers.InterfacesModelSerializer
        filter_backends = [SearchFilter, OrderingFilter]
        search_fields = ['^name', '=tester']
        ordering_fields = ['id', 'name', 'tester']
        pagination_class = PageNumberPagination
    
        @action(methods=['get'], detail=False)
        def GetTester(self, request, *args, **kwargs):
            queryset = self.get_queryset()
            tester_list = [{'tester': item.tester} for item in queryset]
            return Response(tester_list, status=status.HTTP_200_OK)

    二、swagger接口文档

    1、安装

    pipenv install drf-yasg

    2、settings.py中添加

    INSTALLED_APPS =
    
    [ 。。。
    
    'drf_yasg', 
    
     。。。]

    3、urls.py中添加

    from drf_yasg.views import get_schema_view
    from drf_yasg import openapi
    
    schema_view = get_schema_view(
        openapi.Info(
            title="Lemon API接口文档平台",    # 必传
            default_version='v1',   # 必传
            description="这是一个接口文档",
            terms_of_service="http://api.keyou.site",
            contact=openapi.Contact(email="keyou100@qq.com"),
            license=openapi.License(name="BSD License"),
        ),
        public=True,
        # permission_classes=(permissions.AllowAny,),   # 权限类
    )
    
    urlpatterns = [
        re_path(r'^swagger(?P<format>.json|.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
        path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
        path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
    ]

  • 相关阅读:
    RAID卡是否有(启用)缓存对“随机读写”性能有巨大的影响。
    《C++程序设计实践与技巧:测试驱动开发》 环境搭建遇到的坑
    c++ 实现 cout 示例
    c++ 文件
    js 鼠标事件模拟
    eclipse c++ 配置 c++ 17
    c++ 17 vector中string的性能问题 std::vector<std::string> string vs string_view
    c++ 17 模板
    C++17剖析:string在Modern C++中的实现
    编译程序加不加 -lpthread 的区别
  • 原文地址:https://www.cnblogs.com/erchun/p/14452058.html
Copyright © 2011-2022 走看看