Home >Backend Development >Python Tutorial >How to use query parameters to filter data in FastAPI
How to use query parameters to filter data in FastAPI
Introduction:
FastAPI is a modern, fast (high-performance) web framework based on Python. It's easy to use and performs very well. This article will introduce how to use query parameters to filter data in FastAPI to achieve flexible query of data.
1. Start the FastAPI application
First, we need to create a FastAPI application and start it, as shown below:
from fastapi import FastAPI app = FastAPI() @app.get("/") async def root(): return {"message": "Hello, FastAPI!"}
In the above code, we created a FastAPI application Application and defines a root route /
. By accessing the root route, a simple message "Hello, FastAPI!" can be returned.
2. Use query parameters for data filtering
In FastAPI, we can use Query
type parameters to receive query parameters. The following is an example that demonstrates how to use query parameters for data filtering:
from fastapi import FastAPI, Query app = FastAPI() @app.get("/users/") async def get_users(name: str = Query(None)): if name: # 根据查询参数 name 进行数据筛选 result = db.query().filter(User.name == name).all() else: # 若没有查询参数,则返回所有的用户数据 result = db.query().all() return {"users": result}
In the above code, we define a route of /users/
. By accessing this route, we can obtain users data. This route accepts a query parameter name
, which is used to filter user data with the specified name. If the query parameter name
exists, we will use this parameter for data filtering; otherwise, all user data will be returned.
3. Use multiple query parameters at the same time
In actual scenarios, we may need to filter data based on multiple query parameters at the same time. Here is an example that demonstrates how to use multiple query parameters at the same time for data filtering:
from fastapi import FastAPI, Query app = FastAPI() @app.get("/users/") async def get_users(name: str = Query(None), age: int = Query(None)): query = db.query() if name: query = query.filter(User.name == name) if age: query = query.filter(User.age == age) result = query.all() return {"users": result}
In the above code, we define a route of /users/
and accept two queries Parameters name
and age
. We can filter data based on these two query parameters at the same time to achieve more flexible query operations.
4. Use default values and verification
FastAPI also supports setting default values for query parameters and performing verification. For example, we can set the default value for the age
query parameter to 20
, and limit the value range of age
to 10
to ## Between #50:
from fastapi import FastAPI, Query app = FastAPI() @app.get("/users/") async def get_users(name: str = Query(None), age: int = Query(20, ge=10, le=50)): query = db.query() if name: query = query.filter(User.name == name) query = query.filter(User.age == age) result = query.all() return {"users": result}In the above code, we specify
age The default value of the query parameter is
20, and use
ge The and
le parameters are verified for
age. In this way, when the
age query parameter is not specified in the request, the default value
20 will be automatically used; and when the
age query parameter is specified, the value range will be of verification.
Through the above sample code, we have learned how to use query parameters for data filtering in FastAPI. Using query parameters can add flexibility to our application, allowing us to freely filter data according to specific needs. At the same time, FastAPI provides a wealth of functions and options, making the use of query parameters more powerful and convenient. I hope this article can help you learn and use FastAPI.
The above is the detailed content of How to use query parameters to filter data in FastAPI. For more information, please follow other related articles on the PHP Chinese website!