mirror of
https://github.com/fastapi/fastapi.git
synced 2025-12-25 07:08:11 -05:00
* 📝 Add docs recommending Union over Optional * 📝 Update docs recommending Union over Optional * 📝 Update source examples for docs, recommend Union over Optional * 📝 Update highlighted lines with updated source examples * 📝 Update highlighted lines in Markdown with recent code changes * 📝 Update docs, use Union instead of Optional * ♻️ Update source examples to recommend Union over Optional * 🎨 Update highlighted code in Markdown after moving from Optional to Union
50 lines
868 B
Python
50 lines
868 B
Python
from typing import Any, List, Union
|
|
|
|
import peewee
|
|
from pydantic import BaseModel
|
|
from pydantic.utils import GetterDict
|
|
|
|
|
|
class PeeweeGetterDict(GetterDict):
|
|
def get(self, key: Any, default: Any = None):
|
|
res = getattr(self._obj, key, default)
|
|
if isinstance(res, peewee.ModelSelect):
|
|
return list(res)
|
|
return res
|
|
|
|
|
|
class ItemBase(BaseModel):
|
|
title: str
|
|
description: Union[str, None] = None
|
|
|
|
|
|
class ItemCreate(ItemBase):
|
|
pass
|
|
|
|
|
|
class Item(ItemBase):
|
|
id: int
|
|
owner_id: int
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
getter_dict = PeeweeGetterDict
|
|
|
|
|
|
class UserBase(BaseModel):
|
|
email: str
|
|
|
|
|
|
class UserCreate(UserBase):
|
|
password: str
|
|
|
|
|
|
class User(UserBase):
|
|
id: int
|
|
is_active: bool
|
|
items: List[Item] = []
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
getter_dict = PeeweeGetterDict
|