Pagination for Tortoise-ORM on FastAPI
Project description
Usage
Supposing in myapp.schema you have a pydantic BaseModel to represent your model
from fastapi import Depends
from tortoise_pagination import Pagination, Page
from myapp.main import app
from myapp.models import MyModel
from myapp.schema import MySchema
@app.get('/mymodel')
async def my_view(pagination: Depends(Pagination.from_query)) -> Page[MySchema]:
return await pagination.paginated_response(MyModel.all(), MySchema)
now you can request with:
curl http://localhost:8000/mymodel?offset=0&limit=20
returned structure:
- items list[MySchema]
- count: NonNegativeInt -> the number of entries for this queryset
(
MyModel.all().count()) wich the frontend will need to be able to display a pagination
Computed field
Sometime you may want to add computed fields, however pydantic is not async capable so sometime it may become a challenge to achieve that we do:
from fastapi import Depends
from fastapi.routing import APIRouter
from myapp.models import Product
from tortoise.contrib.pydantic import pydantic_model_creator
from tortoise_pagination import Page, Pagination
ProductBaseSchema = pydantic_model_creator(Product, include=("id", "price", "weight"))
class ProductSchema(ProductBaseSchema):
name: str
price_per_kilogram: float | None
router = APIRouter(prefix="products")
async def _compute_price_per_kilogram(product: Product) -> float | None:
try:
return product.price / product.weight
except ZeroDivisionError:
return None
@router.get("")
async def list_products(
pagination: Depends(Pagination.from_query),
) -> Page[ProductSchema]:
products = Products.all()
return await pagination.get_custom_paginated_response(
queryset=products,
schema=ProductSchema,
extra_fields={
"name": lambda product: product.name.title(),
"price_per_kilogram": _compute_price_per_kilogram,
},
)
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tortoise_pagination-1.2.9.tar.gz.
File metadata
- Download URL: tortoise_pagination-1.2.9.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.10.8-arch1-1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
46f1c2321e1d90339957fac6d51ac39e20bbed0ab2726ff45632d3bb3b6a2e4c
|
|
| MD5 |
e965b7b7d0b1976fbbd823342b4ca1a7
|
|
| BLAKE2b-256 |
541ba27e91dd2c4317e2072ef3686aab929cb72c22acc2e27898db5926be1f1e
|
File details
Details for the file tortoise_pagination-1.2.9-py3-none-any.whl.
File metadata
- Download URL: tortoise_pagination-1.2.9-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.10.8-arch1-1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5fef4d02ec1354829e98cfc37575ca8e34a23705ebdca08194c397fddfda435b
|
|
| MD5 |
3512495a1269257d5c1169966b92dd58
|
|
| BLAKE2b-256 |
064f3a08b182af17b78e0c650b5f64ba5af85ee8129775a29da3f55e99d99a33
|