Support multiple Query() in a single endpoint #12212
Replies: 9 comments 5 replies
-
|
Seems like you can only use from typing import Annotated
from fastapi import Depends, FastAPI, Query
from pydantic import BaseModel, Field
app = FastAPI()
# new expected way of using of Query() with pydantic models since 0.115.0
class PaginationNew(BaseModel):
limit: int = Field(100, gt=0, lt=1000, description="Limit the number of items.")
offset: int = Field(0, ge=0, description="Start showing items from this index.")
class OrderingNew(BaseModel):
order_by: str | None = Field(None, description="Order the results by this field.")
descending: bool = Field(
False,
description="If true, return the results in descending order, ascending otherwise.",
)
class AllParams(PaginationNew, OrderingNew):
pass
@app.get(path="/new_logs")
def paginate_new_logs(
query_params: Annotated[AllParams, Query()],
) -> dict:
return {"pagination": []} |
Beta Was this translation helpful? Give feedback.
-
|
The default behavior when having more than one Query model in the function signature is a bit odd, looks like a bug that should be fixed. I would expect to have all the query params flattened like in the previous screenshot without having to use the workaround. |
Beta Was this translation helpful? Give feedback.
-
|
In addition, it seems like once you use a pydantic model you can't combine it with other query params from 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] = []
@app.get("/items/")
async def read_items(filter_query: Annotated[FilterParams, Query()], x: str):
return filter_queryWhen making the request we get this error: {
"detail": [
{
"loc": [
"query",
"filter_query"
],
"msg": "Field required",
"type": "missing"
}
]
} |
Beta Was this translation helpful? Give feedback.
-
|
It seems like this should be an issue. |
Beta Was this translation helpful? Give feedback.
-
|
/cc @tiangolo |
Beta Was this translation helpful? Give feedback.
-
|
This really needs looking at, the feature is pretty much unusable in its current state for us. Can someone with privileges make this an issue so it can be looked into, please? |
Beta Was this translation helpful? Give feedback.
-
|
I also like the idea of supporting multiple Query(). On a (potentially) related note, I'm having trouble getting the list to work in the documentation example for this new feature. In the example, tags is a list of strings, so I expected (and hoped for) the same usage as described here where the parameter can be passed multiple times in order to build the list (e.g. http://localhost:8000/items/?tags=foo&tags=bar would result in Is that not the case? |
Beta Was this translation helpful? Give feedback.
-
|
This issue should be resolved in scope of #12944 PR |
Beta Was this translation helpful? Give feedback.
-
|
For those still waiting for the official FastAPI fix - you can use pip install fastapi-backportsBrings multiple Query models and PEP 695 type alias support to https://github.com/uriyyo/fastapi-backports Usage example: import fastapi_backports.apply # noqa: F401
from typing import Annotated, Any
from fastapi import FastAPI, Query
from pydantic import BaseModel
app = FastAPI()
class UserFilters(BaseModel):
min_age: int | None = None
max_age: int | None = None
class GroupFilters(BaseModel):
min_size: int | None = None
max_size: int | None = None
type UserFiltersDependency = Annotated[
UserFilters,
Query(),
]
type GroupFiltersDependency = Annotated[
GroupFilters,
Query(),
]
@app.get("/filters")
async def get_filters(
user_filters: UserFiltersDependency,
group_filters: GroupFiltersDependency,
) -> dict[str, Any]:
return {
"user_filters": user_filters,
"group_filters": group_filters,
}GET /filters?min_age=10&max_size=20
{
"user_filters": {
"min_age": 10,
"max_age": null
},
"group_filters": {
"min_size": null,
"max_size": 20
}
} |
Beta Was this translation helpful? Give feedback.


Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
When I define multiple
Query()in a single endpoint using the new way of using pydantic models, the generated final query parameters from the endpoint do not work as I expected or it is not yet fully supported.Using
Query()in the two parameters this is what I get (new expected way of doing since0.115.0):Field()in each attribute in the pydantic modelField(Query())in each attribute in the pydantic modelUsing
Depends()for both query parameters, as I was using it, before the recommended way since the0.115.0update:Field()in each attribute in the pydantic modelField(Query())in each attribute in the pydantic modelThe question basically is:
Query()parameters if it is not usingDepends()with pydantic models defined asField(Query())?Operating System
Linux
Operating System Details
Ubuntu 22.04
FastAPI Version
0.115.0
Pydantic Version
2.9.2
Python Version
Python 3.11.8
Beta Was this translation helpful? Give feedback.
All reactions