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

Uploaded Source

Built Distribution

fastapi_filters-0.2.9-py3-none-any.whl (18.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_filters-0.2.9.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.8.18 Linux/6.2.0-1016-azure

File hashes

Hashes for fastapi_filters-0.2.9.tar.gz
Algorithm Hash digest
SHA256 3299cccdf12d12d40a2ce4bedf19248ecb74c7f2e1fd1908e09b1656f7678cf2
MD5 96512e25393124650d67b27d2b3dffdc
BLAKE2b-256 e8f0de252c56ad6dcbee2d8f612eb08ef564db98e0437a3853d01ae9a253caeb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastapi_filters-0.2.9-py3-none-any.whl
  • Upload date:
  • Size: 18.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.8.18 Linux/6.2.0-1016-azure

File hashes

Hashes for fastapi_filters-0.2.9-py3-none-any.whl
Algorithm Hash digest
SHA256 98449639116b219945a6149eecdc75187617dc02923a0b08324f91d8af7304e5
MD5 f3b0c1a8f2c0f40786741ecf9feea323
BLAKE2b-256 34a104cababf8fd451476f4bcc43967a1f40fdb85181d6d696c853a95a763d17

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