Dynamic and declarative query filters for FastAPI, powered by Pydantic v2 and ready for multiple ORMs.
Project description
:rocket: fastapi-query-filters
Dynamic and declarative query filters for FastAPI, powered by Pydantic v2 and ready for multiple ORMs.
Features
- 🔍 Dynamic Filtering: Generate powerful query filters from Pydantic models automatically
- 🎯 Declarative API: Clean, intuitive syntax for defining filters
- 🔌 Multi-ORM Support: Built-in support for SQLAlchemy with extensibility for other ORMs
- 📦 Pydantic v2: Full integration with Pydantic v2 for robust data validation
- ⚡ FastAPI Native: Seamlessly integrates with FastAPI dependencies
- 🔤 Search & Sort: Support for text search and custom sorting operators
- 🌳 Multi-level Relationships: Filter through nested models at any depth
- 🛡️ Strict Mode: Optional strict validation for unknown query parameters
- 🧪 Type Safe: Full type hints and mypy strict mode support
Installation
# Standard installation
pip install fastapi-query-filters
# Installation with SQLAlchemy support
pip install "fastapi-query-filters[sqlalchemy]"
Note: If you are installing directly from GitHub:
pip install "fastapi-query-filters[sqlalchemy] @ git+https://github.com/ezeparziale/fastapi-query-filters.git"
Requirements
- Python >= 3.11
- FastAPI >= 0.110.0
- Pydantic >= 2.0.0
- SQLAlchemy >= 2.0.0
Quick Start
1. Define Your Schema
from datetime import datetime
from pydantic import BaseModel, ConfigDict, EmailStr, Field
class UserOut(BaseModel):
id: int = Field(json_schema_extra={"filters": ["eq"]})
email: EmailStr = Field(json_schema_extra={"filters": ["eq", "icontains"]})
profile_bio: str | None = Field(
None,
json_schema_extra={"filters": ["eq", "icontains", "isnull"]},
)
model_config = ConfigDict(from_attributes=True)
class PostOut(BaseModel):
id: int = Field(json_schema_extra={"filters": ["eq", "gte", "lte", "in"]})
title: str = Field(json_schema_extra={"filters": ["eq", "icontains"]})
created_at: datetime = Field(json_schema_extra={"filters": ["gte", "lte"]})
is_active: bool = Field(json_schema_extra={"filters": ["eq"]})
user_id: int = Field(json_schema_extra={"filters": ["eq"]})
author: UserOut
model_config = ConfigDict(from_attributes=True)
class FilterConfig:
search_field = "q"
sort_field = "sort_by"
enable_sort = True
enable_search = True
prefix = ""
search_columns = ["title", "description"]
2. Use in Your Endpoint
from fastapi import Depends, FastAPI
from fastapi_query_filters import FilterDep, FilterValues
from fastapi_query_filters.orm.sqlalchemy import apply_filters
app = FastAPI()
@app.get("/posts")
def list_posts(
db: Session = Depends(get_db),
filters: FilterValues = FilterDep(PostOut)
):
stmt = select(Post)
stmt = apply_filters(stmt, Post, filters)
return db.execute(stmt).scalars().all()
3. Query Your API
# Filter by field with eq operator
GET /posts?id__eq=1
# Multiple values for in operator
GET /posts?id__in=1&id__in=2&id__in=3
# Use in operator with comma-separated values
GET /posts?id__in=1,2,3
# Filter by field with icontains operator
GET /posts?title__icontains=python
# Filter by author profile bio NULL / NOT NULL values
GET /posts?author__profile_bio__isnull=true
GET /posts?author__profile_bio__isnull=false
# Multiple filters
GET /posts?published__eq=true&title__icontains=api
# Search across fields
GET /posts?q=fastapi
# Sort results
GET /posts?sort_by=-created_at,title
# Combine filters and search
GET /posts?id__in=1,2,3&is_active__eq=true&q=fastapi&sort_by=-created_at
Supported Operators
The library supports the following filter operators for different field types:
| Operator | Description | Example | Types |
|---|---|---|---|
eq |
Equal | id__eq=1 |
all |
ne |
Not equal | status__ne=inactive |
all |
gt |
Greater than | age__gt=18 |
int, datetime |
lt |
Less than | age__lt=65 |
int, datetime |
gte |
Greater than or equal | created_at__gte=2024-01-01 |
int, datetime |
lte |
Less than or equal | price__lte=100 |
int, datetime |
in |
In list | id__in=1,2,3 |
all |
not_in |
Not in list | status__not_in=deleted,archived |
all |
like |
SQL LIKE pattern | name__like=%john% |
str |
ilike |
SQL ILIKE (case-insensitive) | email__ilike=%gmail% |
str |
icontains |
Case-insensitive contains | title__icontains=python |
str |
isnull |
Check if value is NULL or NOT NULL (supports true/false or 1/0) | status__isnull=true |
all |
FilterConfig Configuration
Customize filter behavior using the FilterConfig nested class in your schema:
class PostOut(BaseModel):
# ... fields ...
class FilterConfig:
# Query parameter name for global search (default: "q")
search_field = "q"
# Query parameter name for sorting (default: "sort_by")
sort_field = "sort_by"
# Global prefix for all filter parameters (default: "")
prefix = ""
# Enable/disable global search functionality (default: True)
enable_search = True
# Enable/disable sorting functionality (default: True)
enable_sort = True
# Columns to search when using global search query
search_columns = ["title", "description", "content"]
# Maximum depth for recursive relationship filtering (default: 1)
max_depth = 1
# Whether to forbid all unknown query parameters (default: False)
strict = False
# Optional: Pydantic model with virtual/extra filter fields
extra_filters = PostFilterExtra
FilterConfig Parameters Explained
- search_field: The query parameter name used for global search across multiple columns
- sort_field: The query parameter name used to specify sorting order
- prefix: A prefix prepended to all filter parameter names (e.g.,
f_id__eq=1) - enable_search: Toggle global search functionality on/off
- enable_sort: Toggle dynamic sorting functionality on/off
- search_columns: List of database columns to search when using the search_field parameter
- max_depth: Set the recursion limit for relationship filtering. Increase this to allow filtering by fields in nested models (e.g.,
author__team__name__eq). - strict: Enable total strict mode to reject any query parameter not defined in the filter model (returns 422 error).
- extra_filters: A Pydantic model containing additional filter fields from the database that are not included in the main schema output
Advanced Features
Nested Relationship Filtering
Filter by nested relationship fields using double underscore notation:
# Filter posts by author's email
GET /posts?author__email__icontains=example.com
# Filter posts by author's age range
GET /posts?author__age__gte=18&author__age__lte=65
Multi-level Relationships 🌳
Use max_depth to control how deep the relationships can go (default is 1):
class PostOut(BaseModel):
author: UserOut # UserOut has a 'team' relationship
class FilterConfig:
max_depth = 2 # Allow Post -> User -> Team
# Filter posts by author's team name
GET /posts?author__team__name__eq=Medical
Strict Validation Mode 🛡️
By default, unknown query parameters are ignored. Enable strict mode to reject malformed requests or unauthorized parameters:
class FilterConfig:
strict = True
prefix = "f_"
# These will return 422 Unprocessable Entity:
GET /posts?invalid_param=1
GET /posts?f_typo__eq=value
GET /posts?page=1 # Even unrelated params are blocked in total strict mode
Dynamic Sorting
Sort by multiple fields with ascending/descending order:
# Sort by created_at (descending), then by title (ascending)
GET /posts?sort_by=-created_at,title
# Sort by multiple fields
GET /posts?sort_by=-updated_at,id,name
Field Aliases
Use filter_alias in json_schema_extra to map query parameters to different field names:
class PostOut(BaseModel):
title: str = Field(
alias="post_title",
json_schema_extra={
"filters": ["eq", "icontains"],
"filter_alias": "post_title",
},
)
# Query using the alias
GET /posts?post_title__icontains=python
Multiple Query Parameter Values
Multiple values can be passed in different ways:
# Multiple eq operators
GET /posts?id__eq=1&id__eq=2&id__eq=3
# Comma-separated values with in operator
GET /posts?id__in=1,2,3
# Combined search and filters
GET /posts?q=fastapi&is_active__eq=true&author__id__in=1,2,3
Global Prefix
Add a global prefix to all filter parameters for namespace isolation:
class FilterConfig:
prefix = "f_"
# Query with prefix
GET /posts?f_id__eq=1&f_title__icontains=api
Extra Filters (Virtual Fields)
Define additional filter fields that exist in the database but are not included in your main response schema:
class PostFilterExtra(BaseModel):
# Database column available for filtering but not in PostOut response
author__age: int | None = Field(
default=None,
json_schema_extra={"filters": ["gte", "lte", "in"]}
)
# Another database field not exposed in the API response
status: str | None = Field(
default=None,
json_schema_extra={"filters": ["eq", "in"]}
)
class PostOut(BaseModel):
# ... fields exposed in response ...
class FilterConfig:
extra_filters = PostFilterExtra
# Query using extra filter fields
GET /posts?author__age__gte=18
GET /posts?status__eq=draft
Documentation
For detailed documentation and examples, see the examples/ directory.
License
MIT License - see LICENSE file for details.
Author
Ezequiel Parziale
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_query_filters-0.0.7a0.tar.gz.
File metadata
- Download URL: fastapi_query_filters-0.0.7a0.tar.gz
- Upload date:
- Size: 30.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1f1c360abdca2f13be77de952613218b9d9ca716d2b6bc754e420d9eb43342ba
|
|
| MD5 |
365875d303351cb12c99cd16d2fa150e
|
|
| BLAKE2b-256 |
dcb2b19bd9db9b28111273d9dfb37b9de584735bc9fec472008158741a53ae4c
|
File details
Details for the file fastapi_query_filters-0.0.7a0-py3-none-any.whl.
File metadata
- Download URL: fastapi_query_filters-0.0.7a0-py3-none-any.whl
- Upload date:
- Size: 15.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d99f8e7cddae3c474f2fb271d9698d66ccfe30d512eddf8c7d430ef17f4a465b
|
|
| MD5 |
aed54141704eabdfffa6264ae090fea7
|
|
| BLAKE2b-256 |
6e406661f229c08ec9f29abc74b9a601231ecca9b5bf083ef5a22a5027f5b6c0
|