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.2.tar.gz
(13.3 kB
view details)
Built Distribution
File details
Details for the file fastapi_filters-0.2.2.tar.gz
.
File metadata
- Download URL: fastapi_filters-0.2.2.tar.gz
- Upload date:
- Size: 13.3 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e5e6c609acd1de35945cbe1de3d0c120d393fb621b6cbae41e21e308dc12888 |
|
MD5 | 5147a4a090741534c8663e7ee5907dd1 |
|
BLAKE2b-256 | 3e4b68b9ba79f6e3ab13e0386a977a73b8bb4f7229ea8f85d6ce31b142e282f3 |
File details
Details for the file fastapi_filters-0.2.2-py3-none-any.whl
.
File metadata
- Download URL: fastapi_filters-0.2.2-py3-none-any.whl
- Upload date:
- Size: 16.7 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e260d1a54c28de0f0c522c27e8bcd26c71d608242d96ef4979d4a532c99b091 |
|
MD5 | 9ffb59c2979509342c0422d0997e8177 |
|
BLAKE2b-256 | 8d0e460b80ac07cf7902c5ce09903fe477d192fa6b78d6762ae3243f02957b14 |