django rest_framework swagger使用案例

  • 时间:
  • 来源:互联网
  • 文章标签:

环境准备

  环境要求:

  python3

  django2

  pip3

  模块安装:

  pip3 install django-rest-framework

  pip3 install django-rest-swagger

  搭建项目:

  搭建django项目,创建testapi app

参数配置

  setting.py: 

  INSTALLED_APPS中添加:rest_framework,rest_framework_swagger

  

 

视图编辑

  编辑views.py

  

 1 # Create your views here.
 2 # -*- coding: utf-8 -*-
 3 
 4 from rest_framework.views import APIView
 5 
 6 from rest_framework.permissions import AllowAny
 7 from rest_framework.schemas import SchemaGenerator
 8 from rest_framework.schemas.generators import LinkNode, insert_into
 9 from rest_framework.renderers import *
10 from rest_framework_swagger import renderers
11 from rest_framework.response import Response
12 
13 # from rest_framework.schemas import SchemaGenerator
14 class MySchemaGenerator(SchemaGenerator):
15 
16     def get_links(self, request=None):
17         # from rest_framework.schemas.generators import LinkNode,
18         links = LinkNode()
19 
20         paths = []
21         view_endpoints = []
22         for path, method, callback in self.endpoints:
23             view = self.create_view(callback, method, request)
24             path = self.coerce_path(path, method, view)
25             paths.append(path)
26             view_endpoints.append((path, method, view))
27 
28         # Only generate the path prefix for paths that will be included
29         if not paths:
30             return None
31         prefix = self.determine_path_prefix(paths)
32 
33         for path, method, view in view_endpoints:
34             if not self.has_view_permissions(path, method, view):
35                 continue
36             link = view.schema.get_link(path, method, base_url=self.url)
37             # 添加下面这一行方便在views编写过程中自定义参数.
38             link._fields += self.get_core_fields(view)
39 
40             subpath = path[len(prefix):]
41             keys = self.get_keys(subpath, method, view)
42 
43             # from rest_framework.schemas.generators import LinkNode, insert_into
44             insert_into(links, keys, link)
45 
46         return links
47 
48     # 从类中取出我们自定义的参数, 交给swagger 以生成接口文档.
49     def get_core_fields(self, view):
50         return getattr(view, 'coreapi_fields', ())
51 
52 def DocParam(name="default", location="query", required=True, description=None, type="string", *args, **kwargs):
53     return coreapi.Field(name=name, location=location, required=required, description=description, type=type)
54 
55 
56 class ReturnJson(APIView):
61     coreapi_fields = (  #用于swagger doc显示方法必须字符串
62         DocParam("name", description='test'),
63         DocParam("nalanxiao", required=False, description='rohero'),
64     )
65     def get(self, request, *args, **kwargs):
66         json_data = {'name': 'post', 'id': 0}
67         return Response(json_data)
68 
69     def post(self, request, *args, **kwargs):
70         json_data = {'name': 'post', 'id': 0}
71         return Response(json_data)

 

路由设置

  编辑urls.py

  

 1 from django.conf.urls import url
 8 from .views import SwaggerSchemaView, ReturnJson, StudentsApiView
 9 
10 urlpatterns = [
12     url(r'^api/$', ReturnJson.as_view(), name='api'),
13     url(r'^api/v1/$', StudentsApiView.as_view(), name='api_v1'),
17     url(r'^docs/', SwaggerSchemaView.as_view(), name='apiDocs'),
18 ]

效果展示:

  

github:

  https://github.com/Roherolxh/opstest

  觉得有帮助望给个小星星

转载于:https://www.cnblogs.com/rohero/p/10622280.html

本文链接http://www.taodudu.cc/news/show-647667.html