No project description provided
Project description
pydantic-filters
Source Code: https://github.com/so-saf/pydantic-filters
Describe the filters, not implement them! A declarative and intuitive way to describe data filtering and sorting in your application.
The only required dependency is Pydantic. You can use the basic features without being attached to specific frameworks, or use one of the supported plugins and drivers:
Plugins:
- FastAPI >= 0.100.0
Drivers:
- SQLAlchemy >= 2
Installation
pip install pydantic-filters
A Simple Example
BaseFilter
is just a pydantic model, it should be treated similarly
Let's imagine you have a simple user service with the following SQLAlchemy model:
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
class Base(DeclarativeBase):
pass
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str]
age: Mapped[int]
Describe how you would like to filter users using BaseFilter.
from typing import List
from pydantic_filters import BaseFilter
class UserFilter(BaseFilter):
id: List[int]
name: List[str]
name__ilike: str
age__lt: int
age__gt: int
BaseFilter
is just a pydantic model, it should be treated similarly
Next, you need to apply a filter to some query:
from sqlalchemy import select
from pydantic_filters.drivers.sqlalchemy import append_filter_to_statement
statement = select(User)
filter_ = UserFilter(name__ilike="kate", age__lt=23)
stmt = append_filter_to_statement(
statement=statement, model=User, filter_=filter_,
)
And get something like:
SELECT users.id, users.name, users.age
FROM users
WHERE users.name ILIKE 'kate' AND users.age < 23
The filter can be used in conjunction with one of the supported web frameworks:
from typing import Annotated
from fastapi import FastAPI, APIRouter
from pydantic_filters.plugins.fastapi import FilterDepends
router = APIRouter()
@router.get("/")
async def get_multiple(
filter_: Annotated[UserFilter, FilterDepends(UserFilter)],
):
...
app = FastAPI(title="User Service")
app.include_router(router, prefix="/users", tags=["User"])
API Reference
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
File details
Details for the file pydantic_filters-0.3.1.tar.gz
.
File metadata
- Download URL: pydantic_filters-0.3.1.tar.gz
- Upload date:
- Size: 13.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef45a9242837b1cf582e2c7d81ee68fbcb1f89225e7c962b65e1549ac1dd664e |
|
MD5 | fe15b75db5f9800e6c83a51cc5098bb7 |
|
BLAKE2b-256 | e0acb34be2cd25a71a535031413cb85f89d443d07b528806f188744c6bd60829 |
File details
Details for the file pydantic_filters-0.3.1-py3-none-any.whl
.
File metadata
- Download URL: pydantic_filters-0.3.1-py3-none-any.whl
- Upload date:
- Size: 18.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 164b3b88b3bff350dcac0779757e93148b6913d515be4011e5d991834509992e |
|
MD5 | c2012558dc17b62ca60ef532f7012a8e |
|
BLAKE2b-256 | d514a4e62c8996d13b4776872ee89ed03b71c9afaecee9972109a8d40343886e |