Skip to main content

fastapi-filters

Project description

logo

license test codecov downloads pypi black

Introduction

fastapi-filters is a library that provides filtering/sorting feature for FastAPI applications.


Installation

pip install fastapi-filters

Quickstart

To create filters you need either define them manually using create_filters function or automatically generate them based on model using create_filters_from_model function.

from typing import List

from fastapi import FastAPI, Depends
from pydantic import BaseModel, Field

# import all you need from fastapi-filters
from fastapi_filters import create_filters, create_filters_from_model, FilterValues

app = FastAPI()  # create FastAPI app


class UserOut(BaseModel):  # define your model
    name: str = Field(..., example="Steve")
    surname: str = Field(..., example="Rogers")
    age: int = Field(..., example=102)


@app.get("/users")
async def get_users_manual_filters(
    # manually define filters
    filters: FilterValues = Depends(create_filters(name=str, surname=str, age=int)),
) -> List[UserOut]:
    pass


@app.get("/users")
async def get_users_auto_filters(
    # or automatically generate filters from pydantic model
    filters: FilterValues = Depends(create_filters_from_model(UserOut)),
) -> List[UserOut]:
    pass

Currently, fastapi-filters supports SQLAlchemy integration.

from fastapi_filters.ext.sqlalchemy import apply_filters


@app.get("/users")
async def get_users(
    db: AsyncSession = Depends(get_db),
    filters: FilterValues = Depends(create_filters_from_model(UserOut)),
) -> List[UserOut]:
    query = apply_filters(select(UserOut), filters)
    return (await db.scalars(query)).all()

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

fastapi_filters-0.2.1.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

fastapi_filters-0.2.1-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_filters-0.2.1.tar.gz.

File metadata

  • Download URL: fastapi_filters-0.2.1.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.8.17 Linux/5.15.0-1041-azure

File hashes

Hashes for fastapi_filters-0.2.1.tar.gz
Algorithm Hash digest
SHA256 b605a1799f4646f3bb1fca1f491e821d090de986181ed41ab363a79122915ffe
MD5 d59a13103f8a591064da8148d2ec6511
BLAKE2b-256 7a9b42d426aa8c2f6d40c4fa1014b077ec6238680dc995d8fb68f44f1d652a1d

See more details on using hashes here.

File details

Details for the file fastapi_filters-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: fastapi_filters-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.8.17 Linux/5.15.0-1041-azure

File hashes

Hashes for fastapi_filters-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5e2a02fe840d4e59cdeadfc92de8ac376e62a8cd00e168c94cee58e52b2a9f16
MD5 dd35f21d3b9c84e111bbcce0dc7c8504
BLAKE2b-256 1a038c36a4abf42465b5ec41f23ac48d67489ee4589d469290df583533ce4dbf

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