fastapi-filters
Project description
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
Release history Release notifications | RSS feed
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.4.tar.gz
(13.9 kB
view details)
Built Distribution
File details
Details for the file fastapi_filters-0.2.4.tar.gz
.
File metadata
- Download URL: fastapi_filters-0.2.4.tar.gz
- Upload date:
- Size: 13.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.8.18 Linux/6.2.0-1015-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bdbf256d48a0f28de02a83eaaf4c55ac4bda1c87038880881875bd50873257e8 |
|
MD5 | cb867b3c4b456c92aeafc4bec7d2d9f3 |
|
BLAKE2b-256 | 4af4eca5c7e75177d9a0e9d9e721a3ef12117ec5cc97596396e1360c9d7b0935 |
File details
Details for the file fastapi_filters-0.2.4-py3-none-any.whl
.
File metadata
- Download URL: fastapi_filters-0.2.4-py3-none-any.whl
- Upload date:
- Size: 17.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.8.18 Linux/6.2.0-1015-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 61fc17fe900101f204f9c80fae3a1772241f6f0ee9a44039c128e6f4d6f63416 |
|
MD5 | 5b7562c5ade3c3bf35a34c83489aeb17 |
|
BLAKE2b-256 | 1ed4dc548820fdb91e20a3a554d463009364c7d0d628fa73f35e47029eca6191 |