Pydantic

Python data serialization tips

update_forward_refs

from __future__ import annotations
from typing import List
from pydantic import BaseModel


class UserAlone(BaseModel):
    id: int = None
    name: str

    class Config:
        orm_mode = True

class User(UserAlone):
    roles: List['RoleAlone']= []

class RoleAlone(BaseModel):
    id: int = None
    name: str

    class Config:
        orm_mode = True

User.update_forward_refs()

class Role(RoleAlone):
    users: List[UserAlone]

Last updated