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
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_serializer-1.0.0.tar.gz.
File metadata
- Download URL: tortoise_serializer-1.0.0.tar.gz
- Upload date:
- Size: 5.2 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
246d11c55380c1b52cb9eaa9bb889c6ea3870ad36623f4b3bea4411a201d3aa7
|
|
| MD5 |
38deedc9a95fce1800bf73cafe832a09
|
|
| BLAKE2b-256 |
d570c29fa32c42cf3bb5267cf22aa25505521cb35a52733a29f7d6d0240cfeea
|
File details
Details for the file tortoise_serializer-1.0.0-py3-none-any.whl.
File metadata
- Download URL: tortoise_serializer-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.8 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6788a437c22df637a5eafb754c00a4fbf9577251846e8487e5d4edc0bcebead6
|
|
| MD5 |
da9fa008db8f3a01c5a23e06aa31084d
|
|
| BLAKE2b-256 |
987b4558b9eee0cc3daf2e83627bbc1568e7119b8c60e11cefcbb4bfe7188ca5
|