0
0
FastAPIframework~10 mins

Pydantic model basics in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a Pydantic model with a name field.

FastAPI
from pydantic import BaseModel

class User([1]):
    name: str
Drag options to blanks, or click blank then click option'
AModelBase
BBaseModel
CModel
DBase
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong base class name like Model or Base.
Forgetting to inherit from any class.
2fill in blank
medium

Complete the code to create a Pydantic model with an age field of type integer.

FastAPI
from pydantic import BaseModel

class Person(BaseModel):
    age: [1]
Drag options to blanks, or click blank then click option'
Astr
Bbool
Cfloat
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using str for age which should be a number.
Using float which allows decimals.
3fill in blank
hard

Fix the error in the Pydantic model by completing the missing base class.

FastAPI
from pydantic import BaseModel

class Product([1]):
    id: int
    price: float
Drag options to blanks, or click blank then click option'
AModelBase
BModel
CBaseModel
DProductBase
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect base classes that cause runtime errors.
Forgetting to inherit from any class.
4fill in blank
hard

Fill both blanks to create a Pydantic model with a required string field and an optional integer field.

FastAPI
from typing import [1]
from pydantic import BaseModel

class Item(BaseModel):
    name: str
    quantity: [2][int] = None
Drag options to blanks, or click blank then click option'
AOptional
BList
CDict
DSet
Attempts:
3 left
💡 Hint
Common Mistakes
Using List or Dict which expect collections, not optional values.
Not importing Optional from typing.
5fill in blank
hard

Fill all three blanks to define a Pydantic model with a string field, a float field with a default, and a boolean field.

FastAPI
from pydantic import BaseModel

class Config(BaseModel):
    env: [1]
    version: [2] = 1.0
    debug: [3] = False
Drag options to blanks, or click blank then click option'
Astr
Bfloat
Cbool
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up types like using int for version which is a float.
Forgetting to assign default values for optional fields.