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.6.tar.gz
(14.1 kB
view details)
Built Distribution
File details
Details for the file fastapi_filters-0.2.6.tar.gz
.
File metadata
- Download URL: fastapi_filters-0.2.6.tar.gz
- Upload date:
- Size: 14.1 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 | 04f782ea6e507037ee30da47526c7d22cb3a2e639962510c431a31feb4659f2c |
|
MD5 | c3a3ed95dfd4e7b394ccb277c5ec6f06 |
|
BLAKE2b-256 | 9a2b2a40bed70cfbf96de527d557735802ecd28d8c9dc9a085102a9af61bb948 |
File details
Details for the file fastapi_filters-0.2.6-py3-none-any.whl
.
File metadata
- Download URL: fastapi_filters-0.2.6-py3-none-any.whl
- Upload date:
- Size: 18.1 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 | 3d0993e59877badf9610b2cec411fec4b81f3cec854996d752e6b24329de3fcd |
|
MD5 | dff97059752ae0f59d75a6cb0118b3d3 |
|
BLAKE2b-256 | cc2fd439f5f822818bb11c064be06d267b9c26082c45daedd19b3f1cd92b7252 |