A package to simplify filtering and sorting in FastAPI
Project description
Introduction
fastapi-query-tools
is a Python library designed to simplify filtering and sorting in FastAPI applications.
It provides a set of utility functions and data models to help you filter, sort, and order your data to be returned to your clients.
fastapi-query-tools
is built on top of the popular fastapi
library. It is currently designed to work with SQLAlchemy
, but support for other databases may be added in the future.
It is compatible with Python 3.10 and higher.
Features:
- Simplifies filtering and sorting in FastAPI applications.
- Supports a query, a sort by, and an order by system.
- Works with
SQLAlchemy
. - Compatible with Python 3.10 and higher.
Installation
pip install fastapi-query-tools
Quickstart
All you need to do is to add the Filter
dependency to the endpoint params and call filter_and_sort
function
on data you want to filter and/or sort.
from fastapi import FastAPI, Depends
from sqlalchemy import select
# import all you need from fastapi-query-tools
from fastapi_query_tools import Filter, filter_and_sort
app = FastAPI() # create FastAPI app
@app.get('/items')
async def get_items(*, db: Session = Depends(get_db), filter: Filter) -> List[ItemOut]:
stmt = select(Item)
query = filter_and_sort(stmt, filter)
return db.execute(query).scalars().all() # filter and sort data and return
Example Request
curl -X 'GET' \
'http://localhost:8000/api/items?q=item&sort_by=name&order=desc' \
-H 'accept: application/json' \
Where q
is a query, sort_by
is a field (column) to sort by, and order
(asc, desc) is the order of sorting.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file fastapi_query_tools-1.0.1.tar.gz
.
File metadata
- Download URL: fastapi_query_tools-1.0.1.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 03d2c44091cec7fd318fe57b1c4390d5d443683b03d94452eaa4c3cdafc10a56 |
|
MD5 | ed48a5f74660b9554a6b203b047e8dcb |
|
BLAKE2b-256 | 65eb76253190494a4ea73491c862c6c401c353d84c2997e7d6c467a0482952b6 |
File details
Details for the file fastapi_query_tools-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: fastapi_query_tools-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 61029f9aa096dc692d32434dea842d5f89797dafa54d5daf669432484723e932 |
|
MD5 | 3712f88e9b8d6415cefd3ee3752f56d4 |
|
BLAKE2b-256 | 41482ebe2d8e8ae6314ea9ec864e03485cdbaeba97224d25f7cf61b2f00cc5d6 |