django rest framework 自定義額外的參數(shù)

一個好的Django專題少不了您的貢獻(xiàn):點我進(jìn)入專題

自動生成的API文檔參數(shù)不夠用?自己造作呀~(如果有更好的實現(xiàn)歡迎投稿)


效果圖

GET 有兩個方法(list(path:/api/mall/)與retrieve(path:/api/mall/{id}/)生成的),所以and雙重判斷路徑

官方教程地址: autoschema

from ..mlogger import mlogger # 自定義的日志過濾器
import coreapi
from rest_framework.schemas import AutoSchema
# 自定義額外的參數(shù)
class MallViewSchema(AutoSchema):
    def get_manual_fields(self, path, method):
        mlogger.info('path:' + path)
        mlogger.info('method:' + method)
        extra_fields = []
        if method == 'GET' and path == '/api/mall/':
            extra_fields = [
                coreapi.Field(
                    "sss",
                    required=False,
                    location="path",
                    description='Cissty',
                ),
            ]
        manual_fields = super().get_manual_fields(path, method)
        return manual_fields + extra_fields
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容