How to pass and validate dynamic number of filter groups via query parameters?
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didnāt find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google āHow to X in FastAPIā and didnāt find any information.
- I already read and followed all the tutorial in the docs and didnāt find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options š
Example Code
There is no need for code in this example.
Description
Iāve searched for a solution for this seemingly common problem in APIs and have not found an answer. Please point me to the right direction. Iām trying to add dynamic filtering to my api endpoint by passing groups of filters via query parameters:
Say we have a table Person:
id
name: str
age: int
height: int
born: date
I want to create an API endpoint to get list of Persons using dynamic amount of filters
Letās say we define each filter by having three properties:
field
operator
value
depending on type of field we are filtering, weād have different operators:
str: begins, contains, ends
int, date: <, <=, =, >=, >
For example:
filters = {
{field: "name", operator: "contains", value: "test"},
{field: "age", operator: ">", value: 18},
{field: "age", operator: "<=", value: 31},
{field: "height", operator: ">", value: 150},
{field: "height", operator: ">", value: 170},
}
OR
filters = {
{field: "name", operator: "contains", value: "test"},
{field: "age", operator: ">", value: 18},
}
As you can see number of filters is dynamic and can include same field several times.
I need to come up with a way to convert these filters into query parameters and pass it to FastAPI
I would like to have pydantic check that āfieldā value of each filter group is one of those allowed
Also need to make sure that based on type of field, only appropriate operators will be used
Then I could loop through each filter and apply them to query.
How can I achieve this with FastAPI?
How would filters be represented in URI? (I would like to avoid sending them via GET request body)
Is there an alternative way to achieve this?
Operating System
Linux, Windows, macOS
Operating System Details
No response
FastAPI Version
0.70.1
Python Version
3.9.9
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (1 by maintainers)
Top GitHub Comments
Iāve been thinking, and youāre probably better off stringifying the list on the frontend, and then parsing it after you recieve the stringified object, rather than letting FastAPI do it. Something like"
Just tested this locally, stringifying from the frontend as such, and it works as it should. FastAPI get params really arenāt made for this kinda thingā¦ š
Thanks for the help here everyone! š š
Thanks for reporting back and closing the issue @jimbotrue š