Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the BaseModel class from Pydantic.
FastAPI
from pydantic import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Model' instead of 'BaseModel'.
Trying to import 'Schema' which is not a Pydantic class.
✗ Incorrect
The BaseModel class is imported from Pydantic to create data models in FastAPI.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'str' or 'dict' instead of the nested model class.
Using 'int' which is not appropriate here.
✗ Incorrect
The address field in User should be of type Address to nest the model.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Dict' which requires two type parameters.
Using 'Set' or 'Tuple' which are not lists.
✗ Incorrect
The List type from typing is needed to define a list of nested Item models.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' for bio which should be text.
Not using 'Optional' for a field that can be None.
✗ Incorrect
Optional is imported to allow bio to be optional, and its type is str.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Dict' or 'Set' instead of 'List'.
Using a wrong class name instead of 'Product'.
✗ Incorrect
Import List to type the products field as a list of Product models.