Name | tortoise-pagination JSON |
Version |
1.2.11
JSON |
| download |
home_page | None |
Summary | Pagination for Tortoise-ORM on FastAPI |
upload_time | 2024-09-09 13:56:36 |
maintainer | None |
docs_url | None |
author | Sebastien Nicolet |
requires_python | <4.0,>=3.11 |
license | MIT |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Usage
Supposing in `myapp.schema` you have a pydantic `BaseModel` to represent your model
```python
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:
```shell
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:
```python
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,
},
)
```
## Lazy schema
It's possible to build a custom schema from the extra fields definition
```python
from tortoise_pagination import build_pydantic_model_with_extra_fields
def _get_name(product: Product) -> str:
return product.name.title()
async def _compute_price_per_kilogram(product: Product) -> float | None:
try:
return product.price / product.weight
except ZeroDivisionError:
return None
EXTRA_FIELDS = {
"name": _get_name,
"price_per_kilogram": _compute_price_per_kilogram
}
# This model will have `name` and `price_per_kilogram` with the other fields
# note that you must have return annotation types in given function for extra
# fields, otherwise Any will be used (ex for lambda functions).
ProductWithExtraFieldsSchema = build_pydantic_model_with_extra_fields(
ProductSchema,
"ProductWithExtraFields",
EXTRA_FIELDS,
)
```
Raw data
{
"_id": null,
"home_page": null,
"name": "tortoise-pagination",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.11",
"maintainer_email": null,
"keywords": null,
"author": "Sebastien Nicolet",
"author_email": "snicolet95@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/79/dc/46f327c9c7e778887efd44d1d85da1e24b550cb14b0890e680f4be37f5d5/tortoise_pagination-1.2.11.tar.gz",
"platform": null,
"description": "# Usage\nSupposing in `myapp.schema` you have a pydantic `BaseModel` to represent your model\n\n```python\nfrom fastapi import Depends\nfrom tortoise_pagination import Pagination, Page\n\nfrom myapp.main import app\nfrom myapp.models import MyModel\nfrom myapp.schema import MySchema\n\n\n@app.get('/mymodel')\nasync def my_view(pagination: Depends(Pagination.from_query)) -> Page[MySchema]:\n return await pagination.paginated_response(MyModel.all(), MySchema)\n```\n\nnow you can request with:\n```shell\ncurl http://localhost:8000/mymodel?offset=0&limit=20\n```\n\nreturned structure:\n- items list[MySchema]\n- count: NonNegativeInt -> the number of entries for this queryset\n (`MyModel.all().count()`) wich the frontend will need to be able to display a pagination\n\n\n# Computed field\nSometime you may want to add computed fields, however pydantic is not async capable so sometime it may become a challenge\nto achieve that we do:\n\n```python\nfrom fastapi import Depends\nfrom fastapi.routing import APIRouter\nfrom myapp.models import Product\nfrom tortoise.contrib.pydantic import pydantic_model_creator\n\nfrom tortoise_pagination import Page, Pagination\n\nProductBaseSchema = pydantic_model_creator(Product, include=(\"id\", \"price\", \"weight\"))\n\n\nclass ProductSchema(ProductBaseSchema):\n name: str\n price_per_kilogram: float | None\n\n\nrouter = APIRouter(prefix=\"products\")\n\n\nasync def _compute_price_per_kilogram(product: Product) -> float | None:\n try:\n return product.price / product.weight\n except ZeroDivisionError:\n return None\n\n\n@router.get(\"\")\nasync def list_products(\n pagination: Depends(Pagination.from_query),\n) -> Page[ProductSchema]:\n products = Products.all()\n return await pagination.get_custom_paginated_response(\n queryset=products,\n schema=ProductSchema,\n extra_fields={\n \"name\": lambda product: product.name.title(),\n \"price_per_kilogram\": _compute_price_per_kilogram,\n },\n )\n\n```\n\n## Lazy schema\nIt's possible to build a custom schema from the extra fields definition\n```python\nfrom tortoise_pagination import build_pydantic_model_with_extra_fields\n\n\ndef _get_name(product: Product) -> str:\n return product.name.title()\n\n\nasync def _compute_price_per_kilogram(product: Product) -> float | None:\n try:\n return product.price / product.weight\n except ZeroDivisionError:\n return None\n\n\nEXTRA_FIELDS = {\n \"name\": _get_name,\n \"price_per_kilogram\": _compute_price_per_kilogram\n}\n\n# This model will have `name` and `price_per_kilogram` with the other fields\n# note that you must have return annotation types in given function for extra\n# fields, otherwise Any will be used (ex for lambda functions).\nProductWithExtraFieldsSchema = build_pydantic_model_with_extra_fields(\n ProductSchema,\n \"ProductWithExtraFields\",\n EXTRA_FIELDS,\n)\n\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Pagination for Tortoise-ORM on FastAPI",
"version": "1.2.11",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "6486d4d052a3a47ec8152fe1934bd797ad3c4cf45fc2f354adebe44ebb693c88",
"md5": "36843c773a9f54b8f3696be922b37763",
"sha256": "636f8c632db19e79432d1fd152f42ee6c95d75cb12d367b89804130974140079"
},
"downloads": -1,
"filename": "tortoise_pagination-1.2.11-py3-none-any.whl",
"has_sig": false,
"md5_digest": "36843c773a9f54b8f3696be922b37763",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.11",
"size": 4139,
"upload_time": "2024-09-09T13:56:35",
"upload_time_iso_8601": "2024-09-09T13:56:35.104479Z",
"url": "https://files.pythonhosted.org/packages/64/86/d4d052a3a47ec8152fe1934bd797ad3c4cf45fc2f354adebe44ebb693c88/tortoise_pagination-1.2.11-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "79dc46f327c9c7e778887efd44d1d85da1e24b550cb14b0890e680f4be37f5d5",
"md5": "54e584477a9b32efb466b911c1a5c88e",
"sha256": "9fe72e36b601753f1010d0ce103e23c75dcb1b3e18ee62df2b4a637c5f985d58"
},
"downloads": -1,
"filename": "tortoise_pagination-1.2.11.tar.gz",
"has_sig": false,
"md5_digest": "54e584477a9b32efb466b911c1a5c88e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.11",
"size": 3624,
"upload_time": "2024-09-09T13:56:36",
"upload_time_iso_8601": "2024-09-09T13:56:36.542664Z",
"url": "https://files.pythonhosted.org/packages/79/dc/46f327c9c7e778887efd44d1d85da1e24b550cb14b0890e680f4be37f5d5/tortoise_pagination-1.2.11.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-09 13:56:36",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "tortoise-pagination"
}