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.1.0.tar.gz
(10.3 kB
view details)
Built Distribution
File details
Details for the file fastapi_filters-0.1.0.tar.gz
.
File metadata
- Download URL: fastapi_filters-0.1.0.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.9.7 Darwin/22.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97dc48e62ad8b5337414ec508669eb8a9f13740300ef4cc73608b4dac1239fb9 |
|
MD5 | b4f58504a9cee93aea3ce584bc2a03cc |
|
BLAKE2b-256 | f151f93acdbb01b19a8ce3617716617cc78efc2b6266e76cf9ddca8d6240bd85 |
File details
Details for the file fastapi_filters-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: fastapi_filters-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.9.7 Darwin/22.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | da0ca7eecd3d0289425970ef1187550310bdc0a3a82d88ed36837428819daef3 |
|
MD5 | c3329deaba94079f320ecf29c257d854 |
|
BLAKE2b-256 | 026b2e9b77a4cfaf1b4d2dba3704ead8f3423a39ceea5d144ed2918e7a1f93b3 |