โ ๐ท¶
โถ๏ธ โฎ๏ธ โฎ๏ธ ๐ผ, โซ๏ธ ๐ โ โ๏ธ ๐ ๐ 1๏ธโฃ ๐ ๐ท.
๐ โด๏ธ ๐ผ ๐ฉโ๐ป ๐ท, โฉ๏ธ:
- ๐ข ๐ท ๐ช ๐ช โ๏ธ ๐.
- ๐ข ๐ท ๐ ๐ซ โ๏ธ ๐.
- ๐ฝ ๐ท ๐ ๐ฒ ๐ช โ๏ธ #๏ธโฃ ๐.
Danger
๐ ๐ช ๐ฉโ๐ป ๐ข ๐. ๐ง ๐ช "๐ #๏ธโฃ" ๐ ๐ ๐ช โคด๏ธ โ.
๐ฅ ๐ ๐ซ ๐ญ, ๐ ๐ ๐ก โซ๏ธโ "๐#๏ธโฃ" ๐โโ ๐.
๐ ๐ท¶
๐ฅ ๐ข ๐ญ โ ๐ท ๐ช ๐ ๐ โฎ๏ธ ๐ซ ๐ ๐ & ๐ฅ ๐โ ๐ซ โ๏ธ:
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr
app = FastAPI()
class UserIn(BaseModel):
username: str
password: str
email: EmailStr
full_name: Union[str, None] = None
class UserOut(BaseModel):
username: str
email: EmailStr
full_name: Union[str, None] = None
class UserInDB(BaseModel):
username: str
hashed_password: str
email: EmailStr
full_name: Union[str, None] = None
def fake_password_hasher(raw_password: str):
return "supersecret" + raw_password
def fake_save_user(user_in: UserIn):
hashed_password = fake_password_hasher(user_in.password)
user_in_db = UserInDB(**user_in.dict(), hashed_password=hashed_password)
print("User saved! ..not really")
return user_in_db
@app.post("/user/", response_model=UserOut)
async def create_user(user_in: UserIn):
user_saved = fake_save_user(user_in)
return user_saved
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr
app = FastAPI()
class UserIn(BaseModel):
username: str
password: str
email: EmailStr
full_name: str | None = None
class UserOut(BaseModel):
username: str
email: EmailStr
full_name: str | None = None
class UserInDB(BaseModel):
username: str
hashed_password: str
email: EmailStr
full_name: str | None = None
def fake_password_hasher(raw_password: str):
return "supersecret" + raw_password
def fake_save_user(user_in: UserIn):
hashed_password = fake_password_hasher(user_in.password)
user_in_db = UserInDB(**user_in.dict(), hashed_password=hashed_password)
print("User saved! ..not really")
return user_in_db
@app.post("/user/", response_model=UserOut)
async def create_user(user_in: UserIn):
user_saved = fake_save_user(user_in)
return user_saved
๐ **user_in.dict()
¶
Pydantic .dict()
¶
user_in
Pydantic ๐ท ๐ UserIn
.
Pydantic ๐ท โ๏ธ .dict()
๐ฉโ๐ฌ ๐ ๐จ dict
โฎ๏ธ ๐ท ๐ฝ.
, ๐ฅ ๐ฅ โ Pydantic ๐ user_in
๐:
user_in = UserIn(username="john", password="secret", email="john.doe@example.com")
& โคด๏ธ ๐ฅ ๐ค:
user_dict = user_in.dict()
๐ฅ ๐ โ๏ธ dict
โฎ๏ธ ๐ฝ ๐ข user_dict
(โซ๏ธ dict
โฉ๏ธ Pydantic ๐ท ๐).
& ๐ฅ ๐ฅ ๐ค:
print(user_dict)
๐ฅ ๐ ๐ค ๐ dict
โฎ๏ธ:
{
'username': 'john',
'password': 'secret',
'email': 'john.doe@example.com',
'full_name': None,
}
๐ dict
¶
๐ฅ ๐ฅ โ dict
๐ user_dict
& ๐ถโโ๏ธ โซ๏ธ ๐ข (โ๏ธ ๐) โฎ๏ธ **user_dict
, ๐ ๐ "๐" โซ๏ธ. โซ๏ธ ๐ ๐ถโโ๏ธ ๐ & ๐ฒ user_dict
๐ ๐-๐ฒ โ.
, โถ๏ธ โฎ๏ธ user_dict
โช๏ธโก๏ธ ๐, โ:
UserInDB(**user_dict)
๐ ๐ ๐ณ ๐:
UserInDB(
username="john",
password="secret",
email="john.doe@example.com",
full_name=None,
)
โ๏ธ ๐
โซ๏ธโ, โ๏ธ user_dict
๐, โฎ๏ธ โซ๏ธโ ๐ โซ๏ธ ๐ช โ๏ธ ๐ฎ:
UserInDB(
username = user_dict["username"],
password = user_dict["password"],
email = user_dict["email"],
full_name = user_dict["full_name"],
)
Pydantic ๐ท โช๏ธโก๏ธ ๐ โ1๏ธโฃ¶
๐ผ ๐ ๐ฅ ๐ค user_dict
โช๏ธโก๏ธ user_in.dict()
, ๐ ๐:
user_dict = user_in.dict()
UserInDB(**user_dict)
๐ ๐:
UserInDB(**user_in.dict())
...โฉ๏ธ user_in.dict()
dict
, & โคด๏ธ ๐ฅ โ ๐ "๐" โซ๏ธ ๐ถโโ๏ธ โซ๏ธ UserInDB
๐ โฎ๏ธ **
.
, ๐ฅ ๐ค Pydantic ๐ท โช๏ธโก๏ธ ๐ฝ โ1๏ธโฃ Pydantic ๐ท.
๐ dict
& โ ๐จ๐ป¶
& โคด๏ธ โ โ ๐จ๐ป โ hashed_password=hashed_password
, ๐:
UserInDB(**user_in.dict(), hashed_password=hashed_password)
...๐ ๐ ๐โโ ๐:
UserInDB(
username = user_dict["username"],
password = user_dict["password"],
email = user_dict["email"],
full_name = user_dict["full_name"],
hashed_password = hashed_password,
)
Warning
๐ ๐ ๐ข ๐ค ๐ช ๐ง ๐ฝ, โ๏ธ ๐ซ โ๏ธ ๐ซ ๐ ๐ ๐ฐ ๐โโ.
๐ โ¶
๐ ๐ โ 1๏ธโฃ ๐ ๐ญ FastAPI.
๐ โ ๐ ๐ค ๐, ๐โโ โ, ๐ ๐ โ (๐โ ๐ โน 1๏ธโฃ ๐ฅ โ๏ธ ๐ซ ๐), โ๏ธ.
& ๐ ๐ท ๐ ๐ค ๐ ๐ฝ & โ ๐ข ๐ & ๐.
๐ฅ ๐ช ๐ป.
๐ฅ ๐ช ๐ฃ UserBase
๐ท ๐ ๐ฆ ๐งข ๐ ๐ ๐ท. & โคด๏ธ ๐ฅ ๐ช โ ๐ฟ ๐ ๐ท ๐ ๐ ๐ฎ ๐ข (๐ ๐, ๐ฌ, โ๏ธ).
๐ ๐ฝ ๐ ๏ธ, ๐ฌ, ๐งพ, โ๏ธ. ๐ ๐ท ๐.
๐ ๐, ๐ฅ ๐ช ๐ฃ ๐บ ๐ ๐ท (โฎ๏ธ ๐ข password
, โฎ๏ธ hashed_password
& ๐ต ๐):
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr
app = FastAPI()
class UserBase(BaseModel):
username: str
email: EmailStr
full_name: Union[str, None] = None
class UserIn(UserBase):
password: str
class UserOut(UserBase):
pass
class UserInDB(UserBase):
hashed_password: str
def fake_password_hasher(raw_password: str):
return "supersecret" + raw_password
def fake_save_user(user_in: UserIn):
hashed_password = fake_password_hasher(user_in.password)
user_in_db = UserInDB(**user_in.dict(), hashed_password=hashed_password)
print("User saved! ..not really")
return user_in_db
@app.post("/user/", response_model=UserOut)
async def create_user(user_in: UserIn):
user_saved = fake_save_user(user_in)
return user_saved
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr
app = FastAPI()
class UserBase(BaseModel):
username: str
email: EmailStr
full_name: str | None = None
class UserIn(UserBase):
password: str
class UserOut(UserBase):
pass
class UserInDB(UserBase):
hashed_password: str
def fake_password_hasher(raw_password: str):
return "supersecret" + raw_password
def fake_save_user(user_in: UserIn):
hashed_password = fake_password_hasher(user_in.password)
user_in_db = UserInDB(**user_in.dict(), hashed_password=hashed_password)
print("User saved! ..not really")
return user_in_db
@app.post("/user/", response_model=UserOut)
async def create_user(user_in: UserIn):
user_saved = fake_save_user(user_in)
return user_saved
Union
โ๏ธ anyOf
¶
๐ ๐ช ๐ฃ ๐จ Union
2๏ธโฃ ๐, ๐ โ, ๐ ๐จ ๐ ๐ 2๏ธโฃ.
โซ๏ธ ๐ ๐ฌ ๐ โฎ๏ธ anyOf
.
๐, โ๏ธ ๐ฉ ๐ ๐ ๐ typing.Union
:
Note
๐โ โ Union
, ๐ ๐ ๐ฏ ๐ ๐ฅ, โฉ ๐ ๐ฏ ๐. ๐ผ ๐, ๐ ๐ฏ PlaneItem
๐ โญ CarItem
Union[PlaneItem, CarItem]
.
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class BaseItem(BaseModel):
description: str
type: str
class CarItem(BaseItem):
type: str = "car"
class PlaneItem(BaseItem):
type: str = "plane"
size: int
items = {
"item1": {"description": "All my friends drive a low rider", "type": "car"},
"item2": {
"description": "Music is my aeroplane, it's my aeroplane",
"type": "plane",
"size": 5,
},
}
@app.get("/items/{item_id}", response_model=Union[PlaneItem, CarItem])
async def read_item(item_id: str):
return items[item_id]
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class BaseItem(BaseModel):
description: str
type: str
class CarItem(BaseItem):
type: str = "car"
class PlaneItem(BaseItem):
type: str = "plane"
size: int
items = {
"item1": {"description": "All my friends drive a low rider", "type": "car"},
"item2": {
"description": "Music is my aeroplane, it's my aeroplane",
"type": "plane",
"size": 5,
},
}
@app.get("/items/{item_id}", response_model=Union[PlaneItem, CarItem])
async def read_item(item_id: str):
return items[item_id]
Union
๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ¶
๐ ๐ผ ๐ฅ ๐ถโโ๏ธ Union[PlaneItem, CarItem]
๐ฒ โ response_model
.
โฉ๏ธ ๐ฅ ๐ถโโ๏ธ โซ๏ธ ๐ฒ โ โฉ๏ธ ๐ฎ โซ๏ธ ๐ โ, ๐ฅ โ๏ธ โ๏ธ Union
๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ.
๐ฅ โซ๏ธ ๐ โ ๐ฅ ๐ช โ๏ธ โ๏ธ โธ โธ,:
some_variable: PlaneItem | CarItem
โ๏ธ ๐ฅ ๐ฅ ๐ฎ ๐ response_model=PlaneItem | CarItem
๐ฅ ๐ ๐ค โ, โฉ๏ธ ๐ ๐ ๐ ๐ญ โ ๐ ๏ธ ๐ PlaneItem
& CarItem
โฉ๏ธ ๐ฌ ๐ ๐ โ.
๐ ๐ท¶
๐ ๐, ๐ ๐ช ๐ฃ ๐จ ๐ ๐.
๐, โ๏ธ ๐ฉ ๐ typing.List
(โ๏ธ list
๐ 3๏ธโฃ.9๏ธโฃ & ๐):
from typing import List
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: str
items = [
{"name": "Foo", "description": "There comes my hero"},
{"name": "Red", "description": "It's my aeroplane"},
]
@app.get("/items/", response_model=List[Item])
async def read_items():
return items
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: str
items = [
{"name": "Foo", "description": "There comes my hero"},
{"name": "Red", "description": "It's my aeroplane"},
]
@app.get("/items/", response_model=list[Item])
async def read_items():
return items
๐จ โฎ๏ธ โ dict
¶
๐ ๐ช ๐ฃ ๐จ โ๏ธ โ
โ dict
, ๐ฃ ๐ ๐ & ๐ฒ, ๐ต โ๏ธ Pydantic ๐ท.
๐ โ ๐ฅ ๐ ๐ซ ๐ญ โ ๐/๐ข ๐ (๐ ๐ ๐ช Pydantic ๐ท) โช.
๐ ๐ผ, ๐ ๐ช โ๏ธ typing.Dict
(โ๏ธ dict
๐ 3๏ธโฃ.9๏ธโฃ & ๐):
from typing import Dict
from fastapi import FastAPI
app = FastAPI()
@app.get("/keyword-weights/", response_model=Dict[str, float])
async def read_keyword_weights():
return {"foo": 2.3, "bar": 3.4}
from fastapi import FastAPI
app = FastAPI()
@app.get("/keyword-weights/", response_model=dict[str, float])
async def read_keyword_weights():
return {"foo": 2.3, "bar": 3.4}
๐¶
โ๏ธ ๐ Pydantic ๐ท & ๐ โก ๐ ๐ผ.
๐ ๐ซ ๐ช โ๏ธ ๐ ๐ฝ ๐ท ๐ ๐จโ๐ผ ๐ฅ ๐ ๐จโ๐ผ ๐ ๐ช โ๏ธ ๐ "๐ต๐ธ". ๐ผ โฎ๏ธ ๐ฉโ๐ป "๐จโ๐ผ" โฎ๏ธ ๐ต๐ธ โ
password
, password_hash
& ๐
โโ ๐.