Skip to main content

No project description provided

Project description

pydantic-filters

Source Code: https://github.com/so-saf/pydantic-filters

Testing license Linting


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.111.0

Drivers:

  • SQLAlchemy >= 2

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.1.0.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

pydantic_filters-0.1.0-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pydantic_filters-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9bc443c215a2e0971fb092a887386a4439773744fe1ebd224cffa77c769821cd
MD5 9734bc7077c3340d63e8e8239ddba25a
BLAKE2b-256 07d4a6c3e5790b287bac67022bd44d2d3ae500904f5374bd6aff40811a392362

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pydantic_filters-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 98dbb95999e89ee02b6f54c2b1fcc60f899637ec145ade3d317748ddcef353e6
MD5 57e9ae26545c949570afbd87174921ec
BLAKE2b-256 ee31dd0f5a1366fd6485711b0d326431200f5c5d1907d7fb9b9aa6e9998f202f

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