Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/uriyyo/fastapi-filters
FastAPI filters🍸
https://github.com/uriyyo/fastapi-filters
fastapi fastapi-filters fastapi-sqlalchemy filters
Last synced: 24 days ago
JSON representation
FastAPI filters🍸
- Host: GitHub
- URL: https://github.com/uriyyo/fastapi-filters
- Owner: uriyyo
- License: mit
- Created: 2023-01-11T14:19:40.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-12T04:02:54.000Z (7 months ago)
- Last Synced: 2024-04-12T12:01:48.118Z (7 months ago)
- Topics: fastapi, fastapi-filters, fastapi-sqlalchemy, filters
- Language: Python
- Homepage:
- Size: 1.37 MB
- Stars: 46
- Watchers: 1
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Introduction
`fastapi-filters` is a library that provides filtering/sorting feature for [FastAPI](https://fastapi.tiangolo.com/)
applications.----
## Installation
```bash
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.```py
from typing import Listfrom 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, FilterValuesapp = 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.
```py
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()
```