mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
40 lines
774 B
Python
40 lines
774 B
Python
from typing import List
|
|
from pydantic import BaseModel
|
|
|
|
|
|
class PetCls:
|
|
def __init__(self, *, name: str, species: str):
|
|
self.name = name
|
|
self.species = species
|
|
|
|
|
|
class PersonCls:
|
|
def __init__(self, *, name: str, age: float = None, pets: List[PetCls]):
|
|
self.name = name
|
|
self.age = age
|
|
self.pets = pets
|
|
|
|
|
|
class Pet(BaseModel):
|
|
name: str
|
|
species: str
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
|
|
|
|
class Person(BaseModel):
|
|
name: str
|
|
age: float = None
|
|
pets: List[Pet]
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
|
|
|
|
bones = PetCls(name='Bones', species='dog')
|
|
orion = PetCls(name='Orion', species='cat')
|
|
anna = PersonCls(name='Anna', age=20, pets=[bones, orion])
|
|
anna_model = Person.from_orm(anna)
|
|
print(anna_model)
|