Skip to content

➕ 🏷

▶️ ⏮️ ⏮️ 🖼, ⚫️ 🔜 ⚠ ✔️ 🌅 🌘 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
🤓 Other versions and variants
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
🤓 Other versions and variants
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]
🤓 Other versions and variants
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
🤓 Other versions and variants
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}
🤓 Other versions and variants
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 & 🙅‍♂ 🔐.