required是設置必選非必選,nullable允不允許向傳null,location指定參數獲取的位置,可以多選,按前后順序獲取
parser.add_argument('app_id', type=zero_int, required=True, nullable=False, location=['json']) parser.add_argument('user_num', type=empty_str, required=True, nullable=False, location=['json'])
type基本類型可以有:int , zero_int ,str,empty_str, location可以為:form , json等,詳情可查看官方文檔。這里要特別說一下如何獲取前端的list參數,通過action和type結合使用設置,見下面代碼:
parser.add_argument('attachment', action='append', type=str, required=True, nullable=False, location=['json'])
另外,還可以通過 choices= ['day','month']來設置參數可選值,default='xx'來設置默認值
需要注意的是,還支持自定義參數類型校驗,如下例子,自定義一個list類型校驗:
def list_type(value, name): if not value: return [] elif not isinstance(value, list): raise ValueError(name + '參數類型錯誤,必須是list類型') return value
test.add_argument('rd_center_list', type=list_type, required=False, nullable=False, location=['json'])
