fastapi-filters
Project description
Introduction
fastapi-filters is a library that provides filtering/sorting feature for FastAPI
applications.
Installation
pip install fastapi-filters
Quickstart
Define filters using a FilterSet class:
from fastapi import Depends, FastAPI
from pydantic import BaseModel
from fastapi_filters import FilterField, FilterSet, SortingValues, create_sorting
app = FastAPI()
class UserOut(BaseModel):
name: str
surname: str
age: int
class UserFilters(FilterSet):
name: FilterField[str]
surname: FilterField[str]
age: FilterField[int]
@app.get("/users")
async def get_users(
filters: UserFilters = Depends(),
sorting: SortingValues = Depends(create_sorting("name", "age")),
) -> list[UserOut]:
pass
Query parameters are auto-generated based on field types:
GET /users?name[eq]=Steve&age[gt]=30&sort=+age
fastapi-filters supports SQLAlchemy, Tortoise ORM, Beanie (MongoDB), and raw SQL integrations.
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from fastapi_filters.ext.sqlalchemy import apply_filters_and_sorting
@app.get("/users")
async def get_users(
db: AsyncSession = Depends(get_db),
filters: UserFilters = Depends(),
sorting: SortingValues = Depends(create_sorting("name", "age")),
) -> list[UserOut]:
stmt = apply_filters_and_sorting(select(User), filters, sorting)
return (await db.scalars(stmt)).all()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_filters-0.3.3.tar.gz.
File metadata
- Download URL: fastapi_filters-0.3.3.tar.gz
- Upload date:
- Size: 215.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc93bdce653e3472b553dd313bd44dda2c83555b1313326f85b04cf745d8d0df
|
|
| MD5 |
a5ca31e06c505c8513315425bf8a3d87
|
|
| BLAKE2b-256 |
7dc89703b83a526fc3000e5cbbd6709b9bfddce7f2fd3c8411128034d90b8e48
|
File details
Details for the file fastapi_filters-0.3.3-py3-none-any.whl.
File metadata
- Download URL: fastapi_filters-0.3.3-py3-none-any.whl
- Upload date:
- Size: 22.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9842e1b5f9664255e4db0199dbb7ff4c3d6577252cfc7d31047b53438a5ca656
|
|
| MD5 |
734a4c17ef236774221b83424be12015
|
|
| BLAKE2b-256 |
c8c217ab0718e29233983a2b994d34263a5f4b6f69a97080b43ebd02aa66187c
|