django-rest-swagger的優(yōu)化使用方法
如下所示:
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工具結(jié)合Django-rest-framework進(jìn)行restful API的管理以及可視化顯示的時(shí)候,由于swagger2.1以后不再使用yaml文檔描述api,改而使用json描述,雖然swagger有著自動(dòng)適配url掃描生成文檔的能力,可是自動(dòng)生成的文檔并不詳細(xì),然而完全通過json文件描述所有的api,工作量比較大,且有的api也不需要詳細(xì)描述,因而需要自定義api的json描述和自動(dòng)掃描生成相結(jié)合。
實(shí)現(xiàn)如下:
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的優(yōu)化使用方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 詳解Django中views數(shù)據(jù)查詢使用locals()函數(shù)進(jìn)行優(yōu)化
- Django serializer優(yōu)化類視圖的實(shí)現(xiàn)示例
- 淺談優(yōu)化Django ORM中的性能問題
- Django查詢優(yōu)化及ajax編碼格式原理解析
- 詳解Django配置優(yōu)化方法
- Django之使用celery和NGINX生成靜態(tài)頁(yè)面實(shí)現(xiàn)性能優(yōu)化
- 詳解Django的model查詢操作與查詢性能優(yōu)化
- Django代碼性能優(yōu)化與Pycharm Profile使用詳解
- Django程序的優(yōu)化技巧
相關(guān)文章
sklearn和keras的數(shù)據(jù)切分與交叉驗(yàn)證的實(shí)例詳解
這篇文章主要介紹了sklearn和keras的數(shù)據(jù)切分與交叉驗(yàn)證的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-06-06
利用python讀取YUV文件 轉(zhuǎn)RGB 8bit/10bit通用
今天小編就為大家分享一篇利用python讀取YUV文件 轉(zhuǎn)RGB 8bit/10bit通用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-12-12
python實(shí)現(xiàn)對(duì)AES加密的視頻數(shù)據(jù)流解密的方法
密碼學(xué)中的高級(jí)加密標(biāo)準(zhǔn)(Advanced?Encryption?Standard,AES),又稱Rijndael加密法,這篇文章主要介紹了用python實(shí)現(xiàn)對(duì)AES加密的視頻數(shù)據(jù)流解密,需要的朋友可以參考下2023-02-02
Python中類方法@classmethod和靜態(tài)方法@staticmethod解析
這篇文章主要介紹了Python中類方法@classmethod和靜態(tài)方法@staticmethod解析,python中存在三種方法,分別為常規(guī)方法(定義中傳入self)、@classmethod修飾的類方法、@staticmethod修飾的靜態(tài)方法,,需要的朋友可以參考下2023-08-08
Python如何把不同類型數(shù)據(jù)的json序列化
這篇文章主要介紹了Python如何把不同類型數(shù)據(jù)的json序列化,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04

