5
1

More than 1 year has passed since last update.

FastAPIでクエリパラメータの指定にPydanticのModelを使う

Last updated at Posted at 2022-05-17

以下のように Depends() を使えば指定できる

class ArticleFilter(BaseModel):
    tag: str | None = None
    author: str | None = None
    favorited: str | None = None
    limit: int = 20
    offset: int = 0

@router.get("/", response_model=MultipleArticleResponse)
async def read_articles(
    filter: ArticleFilter = Depends()
):
	# TODO implement 
	pass

Swagger見てみるときちんとクエリパラメータとして指定できるようになっている

スクリーンショット 2022-05-17 13.06.16.png


参考
FastAPI query parameter using Pydantic model

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1