<h1 align="center">
<img alt="logo" src="https://raw.githubusercontent.com/uriyyo/fastapi-filters/main/logo.png">
</h1>
<div align="center">
<img alt="license" src="https://img.shields.io/badge/License-MIT-lightgrey">
<img alt="test" src="https://github.com/uriyyo/fastapi-filters/workflows/Test/badge.svg">
<img alt="codecov" src="https://codecov.io/gh/uriyyo/fastapi-filters/branch/main/graph/badge.svg?token=QqIqDQ7FZi">
<a href="https://pepy.tech/project/fastapi-filters"><img alt="downloads" src="https://pepy.tech/badge/fastapi-filters"></a>
<a href="https://pypi.org/project/fastapi-filters"><img alt="pypi" src="https://img.shields.io/pypi/v/fastapi-filters"></a>
<img alt="black" src="https://img.shields.io/badge/code%20style-black-000000.svg">
</div>
## 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 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.
```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()
```
Raw data
{
"_id": null,
"home_page": "https://github.com/uriyyo/fastapi-filters",
"name": "fastapi-filters",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "",
"author": "Yurii Karabas",
"author_email": "1998uriyyo@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/e8/f0/de252c56ad6dcbee2d8f612eb08ef564db98e0437a3853d01ae9a253caeb/fastapi_filters-0.2.9.tar.gz",
"platform": null,
"description": "<h1 align=\"center\">\n<img alt=\"logo\" src=\"https://raw.githubusercontent.com/uriyyo/fastapi-filters/main/logo.png\">\n</h1>\n\n<div align=\"center\">\n<img alt=\"license\" src=\"https://img.shields.io/badge/License-MIT-lightgrey\">\n<img alt=\"test\" src=\"https://github.com/uriyyo/fastapi-filters/workflows/Test/badge.svg\">\n<img alt=\"codecov\" src=\"https://codecov.io/gh/uriyyo/fastapi-filters/branch/main/graph/badge.svg?token=QqIqDQ7FZi\">\n<a href=\"https://pepy.tech/project/fastapi-filters\"><img alt=\"downloads\" src=\"https://pepy.tech/badge/fastapi-filters\"></a>\n<a href=\"https://pypi.org/project/fastapi-filters\"><img alt=\"pypi\" src=\"https://img.shields.io/pypi/v/fastapi-filters\"></a>\n<img alt=\"black\" src=\"https://img.shields.io/badge/code%20style-black-000000.svg\">\n</div>\n\n## Introduction\n\n`fastapi-filters` is a library that provides filtering/sorting feature for [FastAPI](https://fastapi.tiangolo.com/)\napplications.\n\n----\n\n## Installation\n\n```bash\npip install fastapi-filters\n```\n\n## Quickstart\n\nTo create filters you need either define them manually using `create_filters` function or automatically generate them\nbased on model using `create_filters_from_model` function.\n\n```py\nfrom typing import List\n\nfrom fastapi import FastAPI, Depends\nfrom pydantic import BaseModel, Field\n\n# import all you need from fastapi-filters\nfrom fastapi_filters import create_filters, create_filters_from_model, FilterValues\n\napp = FastAPI() # create FastAPI app\n\n\nclass UserOut(BaseModel): # define your model\n name: str = Field(..., example=\"Steve\")\n surname: str = Field(..., example=\"Rogers\")\n age: int = Field(..., example=102)\n\n\n@app.get(\"/users\")\nasync def get_users_manual_filters(\n # manually define filters\n filters: FilterValues = Depends(create_filters(name=str, surname=str, age=int)),\n) -> List[UserOut]:\n pass\n\n\n@app.get(\"/users\")\nasync def get_users_auto_filters(\n # or automatically generate filters from pydantic model\n filters: FilterValues = Depends(create_filters_from_model(UserOut)),\n) -> List[UserOut]:\n pass\n```\n\nCurrently, `fastapi-filters` supports `SQLAlchemy` integration.\n\n```py\nfrom fastapi_filters.ext.sqlalchemy import apply_filters\n\n\n@app.get(\"/users\")\nasync def get_users(\n db: AsyncSession = Depends(get_db),\n filters: FilterValues = Depends(create_filters_from_model(UserOut)),\n) -> List[UserOut]:\n query = apply_filters(select(UserOut), filters)\n return (await db.scalars(query)).all()\n```",
"bugtrack_url": null,
"license": "MIT",
"summary": "fastapi-filters",
"version": "0.2.9",
"project_urls": {
"Homepage": "https://github.com/uriyyo/fastapi-filters",
"Repository": "https://github.com/uriyyo/fastapi-filters"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "34a104cababf8fd451476f4bcc43967a1f40fdb85181d6d696c853a95a763d17",
"md5": "f3b0c1a8f2c0f40786741ecf9feea323",
"sha256": "98449639116b219945a6149eecdc75187617dc02923a0b08324f91d8af7304e5"
},
"downloads": -1,
"filename": "fastapi_filters-0.2.9-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f3b0c1a8f2c0f40786741ecf9feea323",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 18428,
"upload_time": "2023-12-04T16:31:17",
"upload_time_iso_8601": "2023-12-04T16:31:17.141488Z",
"url": "https://files.pythonhosted.org/packages/34/a1/04cababf8fd451476f4bcc43967a1f40fdb85181d6d696c853a95a763d17/fastapi_filters-0.2.9-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e8f0de252c56ad6dcbee2d8f612eb08ef564db98e0437a3853d01ae9a253caeb",
"md5": "96512e25393124650d67b27d2b3dffdc",
"sha256": "3299cccdf12d12d40a2ce4bedf19248ecb74c7f2e1fd1908e09b1656f7678cf2"
},
"downloads": -1,
"filename": "fastapi_filters-0.2.9.tar.gz",
"has_sig": false,
"md5_digest": "96512e25393124650d67b27d2b3dffdc",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 14466,
"upload_time": "2023-12-04T16:31:18",
"upload_time_iso_8601": "2023-12-04T16:31:18.726446Z",
"url": "https://files.pythonhosted.org/packages/e8/f0/de252c56ad6dcbee2d8f612eb08ef564db98e0437a3853d01ae9a253caeb/fastapi_filters-0.2.9.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-04 16:31:18",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "uriyyo",
"github_project": "fastapi-filters",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "fastapi-filters"
}