django rest_framework swagger使用案例

環境準備python

  環境要求:git

  python3github

  django2django

  pip3json

  模塊安裝:api

  pip3 install django-rest-frameworkapp

  pip3 install django-rest-swaggerpost

  搭建項目:ui

  搭建django項目,建立testapi appurl

參數配置

  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

  以爲有幫助望給個小星星

相關文章
相關標籤/搜索