FastAPI filters with standard query parameter syntax (comma-separated values)
Project description
Introduction
fastapi_filters_standard is a fork of fastapi-filters providing filtering and sorting features for FastAPI applications.
This fork introduces standard query parameter syntax for filters:
-
Field operations are now standardized:
field__operationInstead of the old syntax:
field[operation] -
Operations are standardized across the package, independent of SQLAlchemy naming.
-
For filters requiring lists, values are comma-separated in a single parameter:
GET /places?main_industry=1,2,3 -
Optionally, Raw Mode can be used to receive filters exactly as sent in query parameters.
-
Nested filters are now supported, allowing filtering on related objects using syntax like
state__fa_name__icontains=تهران.
Installation
pip install fastapi_filters_standard
Quickstart
Filters can be defined either manually or automatically from Pydantic models.
from typing import List
from fastapi import FastAPI, Depends
from pydantic import BaseModel, Field
from fastapi_filters_standard import create_filters, create_filters_from_model, FilterValues, RawFilterValues
app = FastAPI()
class UserOut(BaseModel):
name: str = Field(..., example="Steve")
surname: str = Field(..., example="Rogers")
age: int = Field(..., example=102)
# Manual filters
@app.get("/users/manual")
async def get_users_manual_filters(
filters: FilterValues = Depends(create_filters(name=str, surname=str, age=int)),
) -> List[UserOut]:
pass
# Automatic filters from Pydantic model
@app.get("/users/auto")
async def get_users_auto_filters(
filters: FilterValues = Depends(create_filters_from_model(UserOut)),
) -> List[UserOut]:
pass
# Raw Mode example
@app.get("/users/raw")
async def get_users_raw_filters(
filters: RawFilterValues = Depends(create_filters_from_model(UserOut, raw_mode=True)),
) -> RawFilterValues:
pass
Nested Filters Example
from fastapi import FastAPI, Depends
from pydantic import BaseModel
from sqlalchemy import select
from db.models import City
from fastapi_filters_standard import create_filters_from_model, FilterValues
from fastapi_filters_standard.ext.sqlalchemy import apply_filters
app = FastAPI()
class StateRead(BaseModel):
fa_name: str
class CityRead(BaseModel):
id: int
fa_name: str
state: StateRead
@app.get("/cities")
async def list_cities(
filters: FilterValues = Depends(
create_filters_from_model(
CityRead,
nested=True,
max_depth=1, # allow filtering one level deep (City -> State)
)
),
):
"""
Example usage:
GET /cities?state__fa_name__icontains=تهران
This produces:
filters = {'state__fa_name': {'icontains': 'تهران'}}
"""
query = apply_filters(select(City), filters, model=City)
result = await db.scalars(query)
return result.all()
This allows filtering cities based on fields of the related state object.
SQLAlchemy Integration
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from fastapi_filters_standard.ext.sqlalchemy import apply_filters
@app.get("/users")
async def get_users(
db: AsyncSession = Depends(get_db),
filters: FilterValues = Depends(create_filters_from_model(UserOut)),
) -> List[UserOut]:
query = apply_filters(select(User), filters)
result = await db.scalars(query)
return result.all()
Raw Mode Example with pytest
import pytest
from fastapi import FastAPI, Depends
from fastapi.testclient import TestClient
from pydantic import BaseModel
from fastapi_filters_standard import create_filters_from_model, RawFilterValues
@pytest.mark.asyncio
async def test_raw_mode():
app = FastAPI()
class UserModel(BaseModel):
username: str
age: int
@app.get("/")
async def route(
filters: RawFilterValues = Depends(create_filters_from_model(UserModel, raw_mode=True))
) -> RawFilterValues:
return filters
client = TestClient(app)
res = client.get("/", params={"username__contains": "09001", "age__gte": "18"})
assert res.status_code == 200
assert res.json() == {"username__contains": "09001", "age__gte": "18"}
Features of this fork
- Standardized query parameter syntax (
field__operation) - Standardized operation names
- Comma-separated list support for multi-value filters
- Full support for filtering and sorting
- Nested filters: filter on related models using double-underscore syntax
- Raw Mode: receive filters exactly as sent, for forwarding to external services
- Compatible with FastAPI and SQLAlchemy async
- Backwards compatible with most
fastapi-filtersfeatures
Project details
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_standard-0.6.1.tar.gz.
File metadata
- Download URL: fastapi_filters_standard-0.6.1.tar.gz
- Upload date:
- Size: 103.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f9f5f6ed952b2465f82101e7d4d802b1796650f29d9b4483d533b2f29f01e5f8
|
|
| MD5 |
f1068a7edab632fb61ac5520c77a3639
|
|
| BLAKE2b-256 |
8fe3f018ddf6d1fd386ce364bac4ed99c3a8e632cc4485615076d34143560942
|
File details
Details for the file fastapi_filters_standard-0.6.1-py3-none-any.whl.
File metadata
- Download URL: fastapi_filters_standard-0.6.1-py3-none-any.whl
- Upload date:
- Size: 27.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca5bcb9f4313cf92379b8802a16247e9f14dffd1174616db9e799518549a1aeb
|
|
| MD5 |
964daa9c93e4db902a4cb59826ca7f5c
|
|
| BLAKE2b-256 |
6f07c58bbf130646b9075dfc69cb16a5485b14bea2fd510545d63cfd8b3461c7
|