Skip to main content

fastapi-filters, just a fork to experiment with

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_wbarnha-0.2.10.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

fastapi_filters_wbarnha-0.2.10-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_filters_wbarnha-0.2.10.tar.gz.

File metadata

File hashes

Hashes for fastapi_filters_wbarnha-0.2.10.tar.gz
Algorithm Hash digest
SHA256 95d700522b8830b192f4e4d8a73955ab49899e2a0c5fe558c0368d19a6053067
MD5 df750a7825f38e0e917841459b80076d
BLAKE2b-256 3956254a5a33c6a72ae4c59dce36b1587dd5530dc04b70b2112f3c0e00d10c3c

See more details on using hashes here.

File details

Details for the file fastapi_filters_wbarnha-0.2.10-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_filters_wbarnha-0.2.10-py3-none-any.whl
Algorithm Hash digest
SHA256 dd47567e0fb49051196e126d0bf91dd52f2a148e089c4387a0399fd4d0f43fe4
MD5 4649de32ced1e36b4e7a2fd2af08913f
BLAKE2b-256 8053547175ca8f342883f16778cbd483a13cbf4ce90146c5f8562669781d31af

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