博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
django rest_framework swagger使用案例
阅读量:5281 次
发布时间:2019-06-14

本文共 3315 字,大约阅读时间需要 11 分钟。

环境准备

  环境要求:

  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 renderers11 from rest_framework.response import Response12 13 # from rest_framework.schemas import SchemaGenerator14 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 included29         if not paths:30             return None31         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                 continue36             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_into44             insert_into(links, keys, link)45 46         return links47 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

你可能感兴趣的文章
内存泄漏调查
查看>>
jquery获取html元素的绝对位置和相对位置的方法
查看>>
谈谈spring
查看>>
ios中webservice报文的拼接
查看>>
Power BI 报告的评论服务支持移动设备
查看>>
HDU 4920 Matrix multiplication
查看>>
ACdream 1068
查看>>
会声会影毛玻璃制作
查看>>
HDU 2665 Kth number
查看>>
CodeChef DGCD Dynamic GCD
查看>>
记叙在人生路上对你影响最大的三位老师
查看>>
002.大数据第二天
查看>>
python装饰器
查看>>
树上的路径
查看>>
【转载】TCP好文
查看>>
系统平均负载
查看>>
问题总结
查看>>
jenkins升级为2.134
查看>>
软件随笔
查看>>
C/C++知识补充 (1)
查看>>