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

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_filters-0.3.1-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_filters-0.3.1.tar.gz
  • Upload date:
  • Size: 151.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.2

File hashes

Hashes for fastapi_filters-0.3.1.tar.gz
Algorithm Hash digest
SHA256 de0665e48c2d6117f0579fde8ef82c3c0e60185b152cc9ccffa2dc554f0cdae6
MD5 51fb5040b28c15132f93ffc875365e5f
BLAKE2b-256 18f4360da88c818a546ad0c1733565f5154c8ae0ddc2b77f923453db9c2920dc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_filters-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 205b09e0914d6f5d711f1c0c6f5df74196c5dca2919eb2f5b1c22be28c686b8a
MD5 205f9796338ebc501300d598302a5fb6
BLAKE2b-256 5569c125fcb309dc8d1f64493fce4239da20d2bf4948d1f6c2cadcd56d6dcef3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page