SQLAlchemy Filterset. An easy way to filter, sort, paginate SQLAlchemy queries
MIT License
Bot releases are visible (Hide)
Published by ymezencev 3 months ago
Renaming of Strategies for Clarity:
Enhanced JoinStrategy:
Introduction of MultiJoinStrategy:
This filter demonstrates how to implement a custom filter using the new MultiJoinStrategy:
class ParentsTagFilter(Filter):
def filter(self, query, value, values):
expression = or_(Parent.name == value, GrandParent.name == value))
return self.strategy.filter(query, expression)
This filter set showcases how to use the new MultiJoinStrategy in combination with JoinStrategy:
class ItemFilterSet(AsyncFilterSet[Item]):
several_joins = MultiJoinStrategy(
JoinStrategy(Parent, Item.parent_id == Parent.right_id),
JoinStrategy(GrandParent, Parent.grand_parent_id == GrandParent.id)
)
id = Filter(Item.id)
ids = InFilter(Item.id)
tag = ParentsTagFilter(strategy=several_joins)
With this update, all joins, whether originating from the base query or added via the RelationJoinStrategy, will be automatically deduplicated.
Suppose you have the following strategy defined:
strategy = RelationJoinStrategy(Parent, onclause=Parent.id == Item.parent_id)
Below are three different ways to construct a query:
strategy.filter(select(Item.id), Parent.name == "test")
strategy.filter(select(Item.id).join(Parent), Parent.name == "test")
strategy.filter(select(Item.id).join(Parent, onclause=Parent.id == Item.parent_id), Parent.name == "test")
With the new join deduplication feature, all of these methods will produce the same optimized query:
SELECT item.id
FROM item
JOIN parent ON parent.id = item.parent_id
WHERE parent.name = 'test'
Published by ymezencev 3 months ago
Published by Mrreadiness over 1 year ago
Simplify imports and dependencies.
Published by Mrreadiness over 1 year ago
First version of sqlalchemy-filterset
!