一、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'), ]