django-rest-swagger的优化使用方法

 更新时间:2019年08月29日 10:19:35   作者:pushiqiang  
今天小编就为大家分享一篇django-rest-swagger的优化使用方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

如下所示:

requirements.txt
django==1.10.5

djangorestframework==3.5.3

django-rest-swagger==2.1.1

参考英文文档:

http://django-rest-swagger.readthedocs.io/en/latest/

使用swagger工具结合Django-rest-framework进行restful API的管理以及可视化显示的时候,由于swagger2.1以后不再使用yaml文档描述api,改而使用json描述,虽然swagger有着自动适配url扫描生成文档的能力,可是自动生成的文档并不详细,然而完全通过json文件描述所有的api,工作量比较大,且有的api也不需要详细描述,因而需要自定义api的json描述和自动扫描生成相结合。

实现如下:

swagger_views.py

# -*- coding: utf-8 -*-

import json
from collections import OrderedDict

from openapi_codec import OpenAPICodec
from openapi_codec.encode import generate_swagger_object
from coreapi.compat import force_bytes

from django.conf import settings

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.schemas import SchemaGenerator

from rest_framework_swagger.renderers import (
 SwaggerUIRenderer,
 OpenAPIRenderer
)


class SwaggerSchemaView(APIView):
 renderer_classes = [
  OpenAPIRenderer,
  SwaggerUIRenderer
 ]

 def load_swagger_json(self, doc):
  """
  加载自定义swagger.json文档
  """
  data = generate_swagger_object(doc)
  with open(settings.API_DOC_PATH) as s:
   doc_json = json.load(s, object_pairs_hook=OrderedDict)

  data['paths'].update(doc_json.pop('paths'))
  data.update(doc_json)
  return OpenAPICodec().decode(force_bytes(json.dumps(data)))

 def get(self, request):
  generator = SchemaGenerator(title='后端API文档',
         urlconf='chess_user.urls')
  schema = generator.get_schema(request=request)
  document = self.load_swagger_json(schema)

  return Response(document)

urls.py

from django.conf.urls import url, include
from django.conf.urls.static import static
from .swagger_views import SwaggerSchemaView


urlpatterns = [
 url(r'^api-doc/$', SwaggerSchemaView.as_view(), name='docs'),

settings.py

SWAGGER_SETTINGS = {
 'JSON_EDITOR': True,
 'LOGIN_URL': 'login',
 'LOGOUT_URL': 'logout',
}

API_DOC_PATH = os.path.join(BASE_DIR, "api-doc/swagger.json")

api-doc/swagger.json

{
 "paths": {
  "v1/user/profile/": {
   "get": {
    "tags": [
     "v1"
    ],
    "description": "用户profile\n",
    "responses": {
     "200": {
      "description": "OK",
      "schema": {
       "title": "User",
       "type": "object",
       "properties": {
        "username": {
         "type": "string"
        },
        "email": {
         "type": "string"
        },
        "phone_number": {
         "type": "string"
        }
       }
      }
     }
    }
   }
  }

 }
}

若有bug,欢迎指出!

以上这篇django-rest-swagger的优化使用方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • python将回车作为输入内容的实例

    python将回车作为输入内容的实例

    今天小编就为大家分享一篇python将回车作为输入内容的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-06-06
  • python编写猜数字小游戏

    python编写猜数字小游戏

    这篇文章主要为大家详细介绍了python编写猜数字小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-10-10
  • sklearn和keras的数据切分与交叉验证的实例详解

    sklearn和keras的数据切分与交叉验证的实例详解

    这篇文章主要介绍了sklearn和keras的数据切分与交叉验证的实例详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06
  • 利用python读取YUV文件 转RGB 8bit/10bit通用

    利用python读取YUV文件 转RGB 8bit/10bit通用

    今天小编就为大家分享一篇利用python读取YUV文件 转RGB 8bit/10bit通用,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • python实现对AES加密的视频数据流解密的方法

    python实现对AES加密的视频数据流解密的方法

    密码学中的高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法,这篇文章主要介绍了用python实现对AES加密的视频数据流解密,需要的朋友可以参考下
    2023-02-02
  • 如何在django中运行scrapy框架

    如何在django中运行scrapy框架

    这篇文章主要介绍了如何在django中运行scrapy框架,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • 浅谈python中真正关闭socket的方法

    浅谈python中真正关闭socket的方法

    今天小编就为大家分享一篇浅谈python中真正关闭socket的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12
  • Python中类方法@classmethod和静态方法@staticmethod解析

    Python中类方法@classmethod和静态方法@staticmethod解析

    这篇文章主要介绍了Python中类方法@classmethod和静态方法@staticmethod解析,python中存在三种方法,分别为常规方法(定义中传入self)、@classmethod修饰的类方法、@staticmethod修饰的静态方法,,需要的朋友可以参考下
    2023-08-08
  • Python+Matplotlib绘制小提琴图的示例代码

    Python+Matplotlib绘制小提琴图的示例代码

    小提琴图 (Violin Plot) 类似纺锤,是一种用来显示数据分布和概率密度的图形,本文为大家介绍了Matplotlib绘制小提琴图的函数源码,需要的可以参考一下
    2023-06-06
  • Python如何把不同类型数据的json序列化

    Python如何把不同类型数据的json序列化

    这篇文章主要介绍了Python如何把不同类型数据的json序列化,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下
    2021-04-04

最新评论