Skip to main content

Pydantic serialization for tortoise-orm

Project description

Tortoise Serializer

Installation

pip add tortoise-serializer

Usage

Reading

from tortoise_serializer import Serializer
from tortoise import Model, fields
from pydantic import Field
from fastapi.routing import APIRouter

class MyUser(Model):
    id = fields.IntegerField(primary_key=True)
    name = fields.CharField(max_length=100, unique=True)


class MyUserSerializer(Serializer):
    id: int
    name: str = Field(max_length=100, description="User unique name")



router = APIRouter(prefix="/users")
@router.get("")
async def get_users() -> list[MyUserSerializer]:
    return await MyUserSerializer.from_queryset(MyUser.all(), context={"user": ...})

(note that you "can" specify context to pass information to serializers but you don't have to)

Writing

from fastapi import Body



class MyUserCreationSerializer(Serializer):
    name: str


@router.post("")
async def create_user(user_serializer: MyUserCreationSerializer = Body(...)) -> MyUserSerializer:
    user = await user_serializer.create_tortoise_instance()
    # here you can also pass `context=` to that function
    return await MyUserSerializer.from_tortoise_orm(user)

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_serializer-1.0.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

tortoise_serializer-1.0.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file tortoise_serializer-1.0.1.tar.gz.

File metadata

  • Download URL: tortoise_serializer-1.0.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.13.1 Linux/6.12.5-arch1-1

File hashes

Hashes for tortoise_serializer-1.0.1.tar.gz
Algorithm Hash digest
SHA256 d13ae945e3950cfc89c8f0cc558f5c52aa8bbb5d4e2861ddda8c12482bea3f47
MD5 775bcb0cb12353fffb4a1e516de8c5a1
BLAKE2b-256 b7e264c9eac2c757593e79dbe16197dc7221ce2e487e4c0ff28e62e9e410e878

See more details on using hashes here.

File details

Details for the file tortoise_serializer-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: tortoise_serializer-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.13.1 Linux/6.12.5-arch1-1

File hashes

Hashes for tortoise_serializer-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 90efbb766cf7bb895e3012bfd6e10d7739a74477bf1984f438927e310849dfc1
MD5 0e0c63e52b25adf6671f44ed15237e01
BLAKE2b-256 0566c437fe9a239b57fce605a284add992fd326db0800a9595eb58b03fa6196b

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