Skip to main content

Implement simple API filter interface for Sqlalchemy,

Project description

Stack:

Installation

pip install pydantic-sqlalchemy-filter

Usage

Implement UserFilter

from pydantic_sqlalchemy_filter import BaseQueryBuilder, DatabaseModel


class UserFilter(BaseQueryBuilder):
    username: str | None = Field(default=None)
    order_by: str | None = Field(default='username', alias='orderBy')

    def _filter(
        self,
        model: DatabaseModel,
        exclude_deleted: bool = False,
        query: sqlalchemy.Select | None = None,
    ) -> sqlalchemy.Select:
        if query is None:
            query = self.build_base_query(
                model=model,
                exclude_deleted=exclude_deleted,
            )

        if self.id is not None
            query = query.filter_by(username=username)

        return query

In FastAPI router:

    @router.get(path='')
    async def get_all_filtered_users(
        query_builder: Annotated[UserFilter, Depends()],
        session: Annotated[AsyncSession, Depends(get_async_session)],
    ) -> list[Users]:
        query = query_builder.build_select_query(model=User)
        results = await session.execute(query)
        users = results.scalars().all()

        return list(users)

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_sqlalchemy_filter-0.0.6.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file pydantic_sqlalchemy_filter-0.0.6.tar.gz.

File metadata

  • Download URL: pydantic_sqlalchemy_filter-0.0.6.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.11.10 Linux/6.5.0-1025-azure

File hashes

Hashes for pydantic_sqlalchemy_filter-0.0.6.tar.gz
Algorithm Hash digest
SHA256 457be8917825975ed39a46bf7f8c3ba78b29121803aedd494672982a60a0782d
MD5 1492535bdce67ed65f3de476ccc60334
BLAKE2b-256 791aca18b979265ceacf2d565302a9c41fabeb969a4860bfb130ac24bdfc37b5

See more details on using hashes here.

File details

Details for the file pydantic_sqlalchemy_filter-0.0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_sqlalchemy_filter-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 d2b1c2f5e5eba6f28b5dfbb87100e24de6f4557fc96acbdc194df8f869b0b269
MD5 5bf81da53733c446e9602138a0fa4085
BLAKE2b-256 f540522c16e2557b5898e0aaa4f536f94aeeb0ad92488dea2f39da244e1c63df

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