An easy way to filter, sort, paginate SQLAlchemy queries
Project description
SQLAlchemy Filterset
An easy way to filter, sort, paginate SQLAlchemy queries
Documentation: https://sqlalchemy-filterset.github.io/sqlalchemy-filterset
Source Code: https://github.com/sqlalchemy-filterset/sqlalchemy-filterset
The library provides a convenient and organized way to filter your database records.
By creating a FilterSet
class, you can declaratively define the filters you want to apply to your SQLAlchemy
queries.
This library is particularly useful in web applications, as it allows users to easily search, filter, sort, and paginate data.
The key features are:
- Declaratively define filters.
- Keep all of your filters in one place, making it easier to maintain and change them as needed.
- Construct complex filtering conditions by combining multiple simple filters.
- It offers a standard approach to writing database queries.
- Reduce code duplication by reusing the same filters in multiple places in your code.
- Sync and Async support of modern SQLAlchemy.
Installation
pip install sqlalchemy-filterset
Requirements: Python 3.7+
SQLAlchemy 1.4+
Basic Usage
The declarative style allows users to easily specify criteria for filtering the records that are
returned from the database by simply setting the attributes of the ProductFilterSet
class.
This can be more convenient and easier to understand than writing raw SQL queries, which
can be more error-prone and difficult to maintain.
Define a FilterSet
In a declarative style, we describe the attributes that will participate in filtering the query in the database:
from sqlalchemy_filterset import FilterSet, Filter, RangeFilter, BooleanFilter
from myapp.models import Product
class ProductFilterSet(FilterSet):
id = Filter(Product.id)
price = RangeFilter(Product.price)
is_active = BooleanFilter(Product.is_active)
Define a FilterSchema
import uuid
from pydantic import BaseModel
class ProductFilterSchema(BaseModel):
id: uuid.UUID | None
price: tuple[float, float] | None
is_active: bool | None
Usage
# Connect to the database
engine = create_engine("postgresql://user:password@host/database")
Base.metadata.create_all(bind=engine)
SessionLocal = sessionmaker(bind=engine)
session = SessionLocal()
# Create the filterset object
filter_set = ProductFilterSet(session, select(Product))
# Define the filter parameters
filter_params = ProductFilterSchema(
price=(10, 100),
is_active=True,
)
# Apply the filters to the query
filtered_products = filter_set.filter(filter_params)
This example will generate the following query:
SELECT product.id, product.title, product.price, product.is_active
FROM product
WHERE product.price >= 10
AND product.price <= 100
AND product.is_active = true
License
This project is licensed under the terms of the MIT license.
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
Hashes for sqlalchemy_filterset-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c2186104858163a70387e30a90e5371487da617c2b1ad71c6e40a02d4254188 |
|
MD5 | dfb2e45bafebc58e9561d652bdcf3e54 |
|
BLAKE2b-256 | d8bd6bca2feac70eee7bd8f4290d6cf9465ba0414e4ce8d22b1530f3fb828f45 |
Hashes for sqlalchemy_filterset-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83e03b941b82d09ed02283c37c07a256290d21054cbcbd4b4387a5d4dca3ded4 |
|
MD5 | 1b0527df99adb3597ae86ac8591f5c7b |
|
BLAKE2b-256 | c466cb007c05fc5983885337adce33ff10fdd146921bc2cc8a2574b749f96f3d |