Can I use more than 1 Query Parameter Model #13566
-
First Check
Commit to Help
Example Codefrom typing import Annotated, Literal
from fastapi import FastAPI, Query
from pydantic import BaseModel, Field
app = FastAPI()
class FilterParams(BaseModel):
limit: int = Field(100, gt=0, le=100)
offset: int = Field(0, ge=0)
order_by: Literal["created_at", "updated_at"] = "created_at"
tags: list[str] = []
class MoreParams(BaseModel):
extra_param: str = Field(
...,
description="An extra parameter",
)
another_param: str = Field(
None,
alias="identifier",
description="Another Parameter",
)
@app.get("/items/")
async def read_items(
filter_params: Annotated[FilterParams, Query()],
more_params: Annotated[MoreParams, Query()]
) -> str:
return "NA"DescriptionI'm having trouble getting multiple query parameter objects to work using the method described in the docs. I've had this working in past versions of FastAPI using Depends() like this: However, this doesn't seem to work correctly. Note that the "tags" parameter is missing from the docs. I'm not sure what I'm doing wrong here and to be honest I am not understanding the difference in semantics between using Query() and using Depends(). Any help will be much appreciated. Thank you. Operating SystemLinux Operating System DetailsNo response FastAPI Version0.115.7 Pydantic Version2.10.6 Python Version3.12.6 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
Hello @tfrangoullides, for now is not possible to use multiple models for Query params, About the difference between Depends and Query (Oversimplify). |
Beta Was this translation helpful? Give feedback.
-
|
@YuriiMotov good day! |
Beta Was this translation helpful? Give feedback.


Hello @tfrangoullides, for now is not possible to use multiple models for Query params,
this is duplicate of #13448.
About the difference between Depends and Query (Oversimplify).
The first one is used to declare a dependency, is not directly related to query but is used sometimes for query params based on how Fastapi construct the dependency chain.
Query before v115 were used to identify 1 to 1 a single query params. From 115 you can use it to identify all fields of a Pydantic Models as query params.