Registry / database / sqlalchemy-filters

sqlalchemy-filters

JSON →
library0.13.0pypypi✓ verified 84d ago

A library to filter SQLAlchemy queries using a JSON-like filter specification. Current version: 0.13.0, supports SQLAlchemy 1.4, Python 3.7+. Release cadence is irregular.

pip install sqlalchemy-filters
INSTALL
IMPORT
SIG · SQLALCHEMY-FILTERS
S
sqlalchemy-filters
databasepythonv0.13.0
Install
3.4s avg
Import
895ms
Disk
41MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.13.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.932s · 43.1MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.4s · import 0.858s · 41MB
41MB installed
● package 41MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

apply_filters
from sqlalchemy_filters import apply_filters
apply_sort
from sqlalchemy_filters import apply_sort

Filters a SQLAlchemy query with a simple filter specification.

from sqlalchemy import create_engine, Column, Integer, String from sqlalchemy.orm import declarative_base, sessionmaker from sqlalchemy_filters import apply_filters, apply_sort Base = declarative_base() class Book(Base): __tablename__ = 'book' id = Column(Integer, primary_key=True) title = Column(String) price = Column(Integer) engine = create_engine('sqlite:///:memory:', echo=False) Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() session.add_all([Book(title='A', price=10), Book(title='B', price=20)]) session.commit() filter_spec = [{'field': 'price', 'op': 'gt', 'value': 15}] query = session.query(Book) filtered_query = apply_filters(query, filter_spec) results = filtered_query.all() print([(b.title, b.price) for b in results])
Debug
Known issues
deprecatedPython 2.7, 3.5, 3.6 support dropped in v0.13.0.
fix
Upgrade to Python 3.7+ and sqlalchemy-filters >=0.13.0.
affects: <=0.12.0
breakingFilter operators '==', '!=' were renamed to 'eq', 'ne' in early versions. Check operator names.
fix
Use 'eq', 'ne', 'gt', 'lt', 'ge', 'le', 'like', 'ilike', 'in', 'not_in', 'any', 'not_any'.
affects: >=0.7.0
gotchaAuto-join behavior: filters on related models automatically join the related table; use the 'auto_join' parameter to disable.
fix
Pass `auto_join=False` to `apply_filters` to prevent automatic joins.
affects: >=0.7.0
gotchaSorting on hybrid attributes requires the attribute to be defined on the model.
fix
Ensure hybrid attributes are properly defined with expressions.
affects: >=0.12.0
Errors
Common errors & fixes
AttributeError: module 'sqlalchemy_filters' has no attribute 'apply_filters'
Incorrect import (e.g., `import sqlalchemy_filters` and then using `sqlalchemy_filters.apply_filters` but the function is not exposed at top level).
fix
Use `from sqlalchemy_filters import apply_filters`.
sqlalchemy.exc.ArgumentError: filter expression is not a valid SQL expression
The filter specification uses an invalid operator or field name.
fix
Check that the field exists on the model and the operator is one of: eq, ne, gt, lt, ge, le, like, ilike, in, not_in, any, not_any.
sqlalchemy.exc.InvalidRequestError: Can't join table/selectable 'some_table' to itself
Auto-join tries to join a table that is already in the query, or the filtering causes a self-referential join.
fix
Disable auto-join with `auto_join=False` or explicitly add the join in the query.
Upgrade
Version history
0.13.0latest on PyPI · released Apr 13, 2023
Audit
Dependencies
sqlalchemyrequiredCore dependency for query filtering.
Agent activity
14 hits · last 30 days
node
12
Meta
1
OpenAI (training)
1
Resources
sqlalchemy-filters — pip install sqlalchemy-filters · libregistry