Skip to main content

pydantic-filters

Testing Coverage PyPI Python License

Define filters as Pydantic models, then translate them into queries with a driver. The package also provides reusable pagination and sorting models and a FastAPI integration.

Documentation: https://so-saf.github.io/pydantic-filters/

Requirements

  • Python 3.10 or newer, including Python 3.14 and 3.15
  • Pydantic 2
  • SQLAlchemy 2 or newer when using the SQLAlchemy driver
  • FastAPI 0.100 or newer when using the FastAPI plugin

Only Pydantic is a required dependency. Install integrations separately:

pip install pydantic-filters
pip install "pydantic-filters" "sqlalchemy>=2"
pip install "pydantic-filters" "fastapi>=0.100"

Quick start

import sqlalchemy as sa
import sqlalchemy.orm as so

from pydantic_filters import BaseFilter
from pydantic_filters.drivers.sqlalchemy import append_filter_to_statement


class Base(so.DeclarativeBase):
    pass


class User(Base):
    __tablename__ = "users"

    id: so.Mapped[int] = so.mapped_column(primary_key=True)
    name: so.Mapped[str]
    age: so.Mapped[int]


class UserFilter(BaseFilter):
    id: list[int]
    name__ilike: str
    age__ge: int


filter_ = UserFilter(name__ilike="kate", age__ge=18)
statement = append_filter_to_statement(
    statement=sa.select(User),
    model=User,
    filter_=filter_,
)

The statement contains conditions equivalent to:

WHERE users.name ILIKE 'kate' AND users.age >= 18

Filter fields are not required by default, and fields that were not supplied are ignored by the driver. See the documentation for:

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

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_filters-0.4.0-py3-none-any.whl (20.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic_filters-0.4.0.tar.gz
  • Upload date:
  • Size: 287.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.1 {"installer":{"name":"uv","version":"0.12.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pydantic_filters-0.4.0.tar.gz
Algorithm Hash digest
SHA256 85715ad866e3c4233143717cb9f9a7d1a2ba1753b6bddb79a744e5b43b3b8149
MD5 9c6de0f617192f99765bf44331fa87d2
BLAKE2b-256 7b9b291e0cec0d4b33cef16c734a41926dacf498a7c56b1ac9701b1c067d3f09

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydantic_filters-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 20.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.1 {"installer":{"name":"uv","version":"0.12.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pydantic_filters-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2ae2c04e1823f46502bb47d5724592f46e30895afbc58bd7abb11e30c934d14a
MD5 86727b52df329ffd1574ff435a785d40
BLAKE2b-256 8d2e041b3c15b0b856f55469dac3c2a6982f25d1ce98a94b30a8a97a2dd69256

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 files

0.3.5

2 files

0.3.4

2 files

0.3.3

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.0

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page