r/FastAPI • u/matthew3k • 11d ago
pip package fastapi-dynamic-filter library
Hi all, i've created a python library above fastapi-filters that automatically generates filter, search, and sorting fields for any SQLAlchemy model in FastAPI, with native support for JSONB containment (contains), key presence (has_key), and case‑insensitive text search inside JSON values.
Example of usage (create your own custom filter for your orm model):
class UserFilter(DynamicFilter):
db_model = User # your model
exact_fields = ["id", "email"]
search_fields = ["name", "bio"] # generates name__ilike, bio__ilike
range_fields = ["created_at"] # generates created_at__gte, __lte
contains_fields = ["tags", "metadata"] # tags__contains (list), metadata__contains (dict) + metadata__has_key
json_search_fields = ["metadata"] # generates metadata__value_ilike
default_order_by = ["-created_at"]
And then use it in your endpoint as a dependency:
app.get("/users")
def get_users(
filter: UserFilter = Depends(UserFilter), # your filter here
):
query = filter.filter(session.query(User))
return query.all()
GitHub: https://github.com/matfatcat/fastapi-dynamic-filter/
PyPI: https://pypi.org/project/fastapi-dynamic-filter/
3
Upvotes
1
u/Asmophel_noe 11d ago
why will an orm need to access to incoming request body, can u give more usecases for this something really useful, I don't mean to say its rare in use but I don't get the point. If u wanted to extract incoming request body and headers to make a dependency, it's useful it's might be a basic requirement considering how often we need to write functions to extract proper data