Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tortoise_pagination-1.2.8.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tortoise_pagination-1.2.8-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file tortoise_pagination-1.2.8.tar.gz.

File metadata

  • Download URL: tortoise_pagination-1.2.8.tar.gz
  • Upload date:
  • Size: 3.1 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

Hashes for tortoise_pagination-1.2.8.tar.gz
Algorithm Hash digest
SHA256 1cca8c0a6f6f826ec8100bb8e5f5947d13e31fb0e6b95bced8a848c557189a14
MD5 33c1c5b1b123a51e0e2fab10205128e5
BLAKE2b-256 10722ea138d913c629be3300e5816ef5a06ceded60ccbac72efb7edafa2abd53

See more details on using hashes here.

File details

Details for the file tortoise_pagination-1.2.8-py3-none-any.whl.

File metadata

  • Download URL: tortoise_pagination-1.2.8-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

Hashes for tortoise_pagination-1.2.8-py3-none-any.whl
Algorithm Hash digest
SHA256 257dc78d20fa47354f1c7aae1120580a11d2754f9d2c05b20a3f16c0caec9d33
MD5 c69054728a2f3171e26fbe097c7d4f2d
BLAKE2b-256 5de2574aee48394ca38de8f2a8bdee1f3c819ae6f8d951b26c02a913d7b3c234

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page