Skip to main content

querymodelling

build consistent api query models for fastapi

Requirements

At least you will need to have the pydantic package installed. Of course it make sense to have some kind of service supporting query models like fast api. Currently sql models are implemented - but feel free to create new ones.

Installation

Example

Typed SQL filter models

SqlFilterModel generates a nested, fully typed filter model (incl. JSON schema) from a model and compiles it into a SQLAlchemy where clause.

from sqlmodel import SQLModel, Field, select
from querymodelling.sql_filter import SqlFilterModel

class ExampleModel(SQLModel, table=True):
    id: int | None = Field(default=None, primary_key=True)
    name: str
    description: str

ExampleFilter = SqlFilterModel(ExampleModel)

query = ExampleFilter.model_validate({
    "and": {
        "name": {"contains": "wurst", "startswith": "bump"},
        "description": {"lower": {"contains": "test"}},
    },
    "or": [{"id": {"in": [1, 2]}}, {"name": {"length": {"gt": 3}}}],
})
statement = query.apply_sql(select(ExampleModel))
# or: select(ExampleModel).where(query.to_sql_expression())
  • Fields of the filter object are combined with AND.
  • and / or accept an object (its conditions are combined) or a list of objects (each object is combined with AND, the list with the operator). not negates an object. Everything can be nested.
  • Operators per type (default): eq, ne, is_null, in, not_in (scalars), gt, gte, lt, lte, between (numbers, dates, times), contains, icontains, startswith, istartswith, endswith, iendswith, like, not_like, ilike (strings).
  • SQL functions per type (default): lower, upper, trim, length (strings), abs (numbers), date, year, month, day, hour, minute (dates / times). Functions return a filter of their result type, e.g. {"name": {"length": {"gt": 3}}}.

Everything is built from generators and can be extended or restricted:

from querymodelling.sql_filter import (
    SqlOperator, SqlFunction, default_sql_filter_mapping, for_types, without
)

def parity(annotation):
    yield SqlOperator(
        "even", lambda column, value: (column % 2 == 0) == value,
        value_type=lambda annotation: bool)

mapping = default_sql_filter_mapping(name_prefix="Custom")
mapping.operator_generators = [
    without(generator, "like", "ilike")
    for generator in mapping.operator_generators
]
mapping.add_operators(for_types(parity, int, exclude=(bool,)))

ExampleFilter = SqlFilterModel(ExampleModel, mapping=mapping)
  • operator generators (annotation) -> Iterable[SqlOperator]
  • function generators (annotation) -> Iterable[SqlFunction]
  • field generators (context, model, name, field_info, annotation) -> Iterable[SqlFilterField] (field_generators=..., e.g. virtual fields based on SQL expressions)
  • logical generators (model) -> Iterable[SqlLogicalOperator] (logical_generators=...)

The filter can be used within existing query models as well:

from querymodelling.pydantic import QueryField
from querymodelling.sql_filter import sql_filter_expression

class ExampleQuery(PageQuery, BaseModel):
    filter: ExampleFilter | None = QueryField(
        sql_filter_expression, default=None)

Metadata

Release files for querymodelling 0.0.17

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for querymodelling 0.0.17
File Size Uploaded Provenance Actions
querymodelling-0.0.17.tar.gz 36.4 kB With provenance Details for querymodelling-0.0.17.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for querymodelling 0.0.17
File Interpreter ABI Platform Actions
querymodelling-0.0.17-py3-none-any.whl Python 3 none any Details for querymodelling-0.0.17-py3-none-any.whl

Total release size: 66.0 kB

Release files / querymodelling-0.0.17.tar.gz

Download URL querymodelling-0.0.17.tar.gz
Size 36.4 kB
Tags Source
SHA-256 checksum
How to use checksums
4929773927121aca26eea26f04779cd2a4dc63158245dc9d461c8b15f74e5aa3
BLAKE2b-256 checksum
How to use checksums
ffbdf15b72add30b59c602e855fa0bc8eaa8752d14b9656cd7b79f94fb999c18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 19, 2026.

Transparency log

Release files / querymodelling-0.0.17-py3-none-any.whl

Download URL querymodelling-0.0.17-py3-none-any.whl
Size 29.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4a73d084dbb5ba096caa4e688155d77decb1828d2b918294021760875303fa19
BLAKE2b-256 checksum
How to use checksums
2145d4958ccae6ffa3f3e62eaa2cf3f931ea969c50ecf246583d6d79beb85073
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 19, 2026.

Transparency log

Security for querymodelling 0.0.17

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for querymodelling-0.0.17.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 19, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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