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.8.tar.gz
(14.5 kB
view details)
Built Distribution
File details
Details for the file fastapi_filters-0.2.8.tar.gz
.
File metadata
- Download URL: fastapi_filters-0.2.8.tar.gz
- Upload date:
- Size: 14.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.8.18 Linux/6.2.0-1015-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c737265720e29ca7c94a587d75cc935c58b39944eeafbc9517f58973e7deafa0 |
|
MD5 | 81e939f402923b9633fa934abe896b0e |
|
BLAKE2b-256 | de0c6df0f2d07dfccfd5e04c93e48fca3ea18b80b37cfa79c66e1f7b05143b5c |
File details
Details for the file fastapi_filters-0.2.8-py3-none-any.whl
.
File metadata
- Download URL: fastapi_filters-0.2.8-py3-none-any.whl
- Upload date:
- Size: 18.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.8.18 Linux/6.2.0-1015-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2dae193129e51353d5d02c4fd6bb6293d837f65bf792f2b556e977f98d4b9c48 |
|
MD5 | d2fcbafd87f35d38b99590ead7c44665 |
|
BLAKE2b-256 | f04aa5689ff1a3d6da5f250e5bd9e1039196dcf75b3a1dabf82a9be513247127 |