0
0
FastAPIframework~10 mins

Pydantic model definition 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 single field 'name' of type string.

FastAPI
from pydantic import BaseModel

class User([1]):
    name: str
Drag options to blanks, or click blank then click option'
ABaseModel
BModel
CSchema
DDataModel
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong base class name like Model or Schema.
Not inheriting from any class.
2fill in blank
medium

Complete the code to add an integer field 'age' to the Pydantic model.

FastAPI
from pydantic import BaseModel

class Person(BaseModel):
    name: str
    age: [1]
Drag options to blanks, or click blank then click option'
Abool
Bstring
Cfloat
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using string or float for age.
Using bool by mistake.
3fill in blank
hard

Fix the error in the Pydantic model by completing the missing import.

FastAPI
from pydantic import [1]

class Product(BaseModel):
    id: int
    price: float
Drag options to blanks, or click blank then click option'
AField
BSchema
CBaseModel
DModel
Attempts:
3 left
💡 Hint
Common Mistakes
Importing wrong classes like Schema or Model.
Forgetting to import BaseModel.
4fill in blank
hard

Fill both blanks to define a Pydantic model with a required string field 'title' and an optional integer field 'year'.

FastAPI
from pydantic import BaseModel

from typing import [1]

class Movie(BaseModel):
    title: str
    year: [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 instead of Optional.
Not importing Optional.
5fill in blank
hard

Fill all three blanks to define a Pydantic model with a field 'tags' that is a list of strings with a default empty list.

FastAPI
from pydantic import BaseModel, [1]
from typing import [2]

class BlogPost(BaseModel):
    tags: [2][str] = [3](default_factory=list)
Drag options to blanks, or click blank then click option'
AField
BList
Clist[str]
DOptional
Attempts:
3 left
💡 Hint
Common Mistakes
Using plain list[str] without importing List.
Not using Field for default values.