MulaiMulai sekarang secara gratis

Pydantic model

You've been asked to create an API endpoint that manages items in inventory. To get started, create a Pydantic model for Items that has attributes name, quantity, and expiration.

Latihan ini adalah bagian dari kursus

Introduction to FastAPI

Lihat Kursus

Petunjuk latihan

  • Import date from datetime and BaseModel from pydantic.
  • Create a Pydantic model for Item.
  • Fill in the following fields correctly: name (string), quantity (integer, optional, default 0), and expiration (date, optional, default None).

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# Import date
from datetime import ____

# Import BaseModel
from pydantic import ____

# Define model Item
class Item(____):
    name: str
    ____: int = 0
    expiration: ____ = None
Edit dan Jalankan Kode