Skip to main content

A declarative and intuitive way to describe data filtering and sorting in your application.

Project description

pydantic-filters

Testing Coverage pypi license versions

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"])

fastapi-simple-example.png

API Reference

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

pydantic_filters-0.3.2.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

pydantic_filters-0.3.2-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_filters-0.3.2.tar.gz.

File metadata

  • Download URL: pydantic_filters-0.3.2.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.6

File hashes

Hashes for pydantic_filters-0.3.2.tar.gz
Algorithm Hash digest
SHA256 87cf1580bd6185e3f7095e8ab71bcfaeab87d175add3c3fa7ef00ce5c53c5882
MD5 0490a61af6eb6ac86a659aef4340f2f8
BLAKE2b-256 279127039126099ed2d5b2f1f96e629248527074df17c9d0d472a2f3822a949d

See more details on using hashes here.

File details

Details for the file pydantic_filters-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_filters-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 df2d955c3548c0428ae7ff1cf8850cd639cbfeea9f13ec13d63da11fab0207f8
MD5 6260e9039db530dab9e38403f46fdeab
BLAKE2b-256 ad44c6f428111196d72fbea7baf96f5b68fb9115aa560bd1fe32906368cdaefc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page