fromtypingimportAnnotatedfromfastapiimportFastAPI,Path,Queryapp=FastAPI()@app.get("/items/{item_id}")asyncdefread_items(item_id:Annotated[int,Path(title="The ID of the item to get")],q:Annotated[str|None,Query(alias="item-query")]=None,):results={"item_id":item_id}ifq:results.update({"q":q})returnresults
fromtypingimportAnnotated,UnionfromfastapiimportFastAPI,Path,Queryapp=FastAPI()@app.get("/items/{item_id}")asyncdefread_items(item_id:Annotated[int,Path(title="The ID of the item to get")],q:Annotated[Union[str,None],Query(alias="item-query")]=None,):results={"item_id":item_id}ifq:results.update({"q":q})returnresults
fromtypingimportUnionfromfastapiimportFastAPI,Path,Queryfromtyping_extensionsimportAnnotatedapp=FastAPI()@app.get("/items/{item_id}")asyncdefread_items(item_id:Annotated[int,Path(title="The ID of the item to get")],q:Annotated[Union[str,None],Query(alias="item-query")]=None,):results={"item_id":item_id}ifq:results.update({"q":q})returnresults
Tip
尽可能选择使用 Annotated 的版本。
fromfastapiimportFastAPI,Path,Queryapp=FastAPI()@app.get("/items/{item_id}")asyncdefread_items(item_id:int=Path(title="The ID of the item to get"),q:str|None=Query(default=None,alias="item-query"),):results={"item_id":item_id}ifq:results.update({"q":q})returnresults
Tip
尽可能选择使用 Annotated 的版本。
fromtypingimportUnionfromfastapiimportFastAPI,Path,Queryapp=FastAPI()@app.get("/items/{item_id}")asyncdefread_items(item_id:int=Path(title="The ID of the item to get"),q:Union[str,None]=Query(default=None,alias="item-query"),):results={"item_id":item_id}ifq:results.update({"q":q})returnresults
fromtypingimportAnnotatedfromfastapiimportFastAPI,Path,Queryapp=FastAPI()@app.get("/items/{item_id}")asyncdefread_items(item_id:Annotated[int,Path(title="The ID of the item to get")],q:Annotated[str|None,Query(alias="item-query")]=None,):results={"item_id":item_id}ifq:results.update({"q":q})returnresults
fromtypingimportAnnotated,UnionfromfastapiimportFastAPI,Path,Queryapp=FastAPI()@app.get("/items/{item_id}")asyncdefread_items(item_id:Annotated[int,Path(title="The ID of the item to get")],q:Annotated[Union[str,None],Query(alias="item-query")]=None,):results={"item_id":item_id}ifq:results.update({"q":q})returnresults
fromtypingimportUnionfromfastapiimportFastAPI,Path,Queryfromtyping_extensionsimportAnnotatedapp=FastAPI()@app.get("/items/{item_id}")asyncdefread_items(item_id:Annotated[int,Path(title="The ID of the item to get")],q:Annotated[Union[str,None],Query(alias="item-query")]=None,):results={"item_id":item_id}ifq:results.update({"q":q})returnresults
Tip
尽可能选择使用 Annotated 的版本。
fromfastapiimportFastAPI,Path,Queryapp=FastAPI()@app.get("/items/{item_id}")asyncdefread_items(item_id:int=Path(title="The ID of the item to get"),q:str|None=Query(default=None,alias="item-query"),):results={"item_id":item_id}ifq:results.update({"q":q})returnresults
Tip
尽可能选择使用 Annotated 的版本。
fromtypingimportUnionfromfastapiimportFastAPI,Path,Queryapp=FastAPI()@app.get("/items/{item_id}")asyncdefread_items(item_id:int=Path(title="The ID of the item to get"),q:Union[str,None]=Query(default=None,alias="item-query"),):results={"item_id":item_id}ifq:results.update({"q":q})returnresults
fromfastapiimportFastAPI,Pathapp=FastAPI()@app.get("/items/{item_id}")asyncdefread_items(q:str,item_id:int=Path(title="The ID of the item to get")):results={"item_id":item_id}ifq:results.update({"q":q})returnresults
fromfastapiimportFastAPI,Pathapp=FastAPI()@app.get("/items/{item_id}")asyncdefread_items(*,item_id:int=Path(title="The ID of the item to get"),q:str):results={"item_id":item_id}ifq:results.update({"q":q})returnresults
fromfastapiimportFastAPI,Pathapp=FastAPI()@app.get("/items/{item_id}")asyncdefread_items(*,item_id:int=Path(title="The ID of the item to get",ge=1),q:str):results={"item_id":item_id}ifq:results.update({"q":q})returnresults
fromfastapiimportFastAPI,Pathapp=FastAPI()@app.get("/items/{item_id}")asyncdefread_items(*,item_id:int=Path(title="The ID of the item to get",gt=0,le=1000),q:str,):results={"item_id":item_id}ifq:results.update({"q":q})returnresults
能够声明 gt 而不仅仅是 ge 在这个前提下变得重要起来。例如,你可以要求一个值必须大于 0,即使它小于 1。
因此,0.5 将是有效值。但是 0.0或 0 不是。
对于 lt 也是一样的。
fromfastapiimportFastAPI,Path,Queryapp=FastAPI()@app.get("/items/{item_id}")asyncdefread_items(*,item_id:int=Path(title="The ID of the item to get",ge=0,le=1000),q:str,size:float=Query(gt=0,lt=10.5),):results={"item_id":item_id}ifq:results.update({"q":q})returnresults