django rest_framework swagger使用案例
2024-09-30 07:49:39
环境准备
环境要求:
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
# Create your views here.
# -*- coding: utf-8 -*- from rest_framework.views import APIView from rest_framework.permissions import AllowAny
from rest_framework.schemas import SchemaGenerator
from rest_framework.schemas.generators import LinkNode, insert_into
from rest_framework.renderers import *
from rest_framework_swagger import renderers
from rest_framework.response import Response # from rest_framework.schemas import SchemaGenerator
class MySchemaGenerator(SchemaGenerator): def get_links(self, request=None):
# from rest_framework.schemas.generators import LinkNode,
links = LinkNode() paths = []
view_endpoints = []
for path, method, callback in self.endpoints:
view = self.create_view(callback, method, request)
path = self.coerce_path(path, method, view)
paths.append(path)
view_endpoints.append((path, method, view)) # Only generate the path prefix for paths that will be included
if not paths:
return None
prefix = self.determine_path_prefix(paths) for path, method, view in view_endpoints:
if not self.has_view_permissions(path, method, view):
continue
link = view.schema.get_link(path, method, base_url=self.url)
# 添加下面这一行方便在views编写过程中自定义参数.
link._fields += self.get_core_fields(view) subpath = path[len(prefix):]
keys = self.get_keys(subpath, method, view) # from rest_framework.schemas.generators import LinkNode, insert_into
insert_into(links, keys, link) return links # 从类中取出我们自定义的参数, 交给swagger 以生成接口文档.
def get_core_fields(self, view):
return getattr(view, 'coreapi_fields', ()) def DocParam(name="default", location="query", required=True, description=None, type="string", *args, **kwargs):
return coreapi.Field(name=name, location=location, required=required, description=description, type=type) class ReturnJson(APIView):
coreapi_fields = ( #用于swagger doc显示方法必须字符串
DocParam("name", description='test'),
DocParam("nalanxiao", required=False, description='rohero'),
)
def get(self, request, *args, **kwargs):
json_data = {'name': 'post', 'id': 0}
return Response(json_data) def post(self, request, *args, **kwargs):
json_data = {'name': 'post', 'id': 0}
return Response(json_data)
路由设置
编辑urls.py
from django.conf.urls import url
from .views import SwaggerSchemaView, ReturnJson, StudentsApiView urlpatterns = [
url(r'^api/$', ReturnJson.as_view(), name='api'),
url(r'^api/v1/$', StudentsApiView.as_view(), name='api_v1'),
url(r'^docs/', SwaggerSchemaView.as_view(), name='apiDocs'),
]
效果展示:
github:
https://github.com/Roherolxh/opstest
觉得有帮助望给个小星星
最新文章
- 使用Executor管理线程
- js 布尔值作为开关判断
- LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- linux基础-第十单元 系统的初始化和服务
- 【androidstudio】将eclipse的项目导入android studio
- angular问题总结与反思
- 每天学点GDB 11
- ios中addtarget
- 通用表表达式(Common Table Expression)
- jQuery实例-简单选项卡-【一些常见方法(2)-练习】
- 客户端技术:Cookie 服务端技术:HttpSession
- windows可以登录qq,但无法打开浏览器页面
- Android切换页面效果的实现二:WebView+ViewPager
- nginx记录响应与POST请求日志
- 转发:iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控
- asp.net core新特性(1):TagHelper
- JDBC操作数据库之查询数据
- MySQL学习4 - 数据类型一
- 爬取伯乐在线文章(五)itemloader
- 从零开始搭建django前后端分离项目 系列一(技术选型)