0
0
FastAPIframework~10 mins

Nested models 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 import the BaseModel class from Pydantic.

FastAPI
from pydantic import [1]
Drag options to blanks, or click blank then click option'
AModel
BField
CSchema
DBaseModel
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Model' instead of 'BaseModel'.
Trying to import 'Schema' which is not a Pydantic class.
2fill in blank
medium

Complete the code to define a nested Pydantic model inside another model.

FastAPI
class Address(BaseModel):
    city: str

class User(BaseModel):
    name: str
    address: [1]
Drag options to blanks, or click blank then click option'
Astr
BAddress
Cint
Ddict
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'str' or 'dict' instead of the nested model class.
Using 'int' which is not appropriate here.
3fill in blank
hard

Fix the error in the nested model definition by completing the missing import.

FastAPI
from pydantic import BaseModel
from typing import [1]

class Item(BaseModel):
    name: str

class Order(BaseModel):
    items: [1][Item]
Drag options to blanks, or click blank then click option'
AList
BDict
CSet
DTuple
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Dict' which requires two type parameters.
Using 'Set' or 'Tuple' which are not lists.
4fill in blank
hard

Fill both blanks to define a nested model with an optional field.

FastAPI
from typing import [1]

class Profile(BaseModel):
    bio: [2] = None
Drag options to blanks, or click blank then click option'
AOptional
Bstr
Cint
DList
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' for bio which should be text.
Not using 'Optional' for a field that can be None.
5fill in blank
hard

Fill all three blanks to create a nested model with a list of nested items and a required field.

FastAPI
from typing import [1]

class Product(BaseModel):
    id: int
    name: str

class Cart(BaseModel):
    products: [2][[3]]
Drag options to blanks, or click blank then click option'
AList
BDict
CProduct
DSet
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Dict' or 'Set' instead of 'List'.
Using a wrong class name instead of 'Product'.