0
0
FastAPIframework~20 mins

Model inheritance in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Model Inheritance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Output of inherited Pydantic model serialization
Given the following FastAPI Pydantic models using inheritance, what is the JSON output of ChildModel().dict()?
FastAPI
from pydantic import BaseModel

class ParentModel(BaseModel):
    id: int = 1
    name: str = "parent"

class ChildModel(ParentModel):
    age: int = 10

result = ChildModel().dict()
A{"id": 1, "name": "parent", "age": 10}
B{"id": 1, "age": 10}
C{"name": "parent", "age": 10}
D{"id": 1, "name": "parent"}
Attempts:
2 left
💡 Hint
Remember that child models inherit all fields from parent models.
📝 Syntax
intermediate
2:00remaining
Correct syntax for extending Pydantic model with extra field
Which option correctly defines a Pydantic model ExtendedModel that inherits from BaseModel and adds a new field extra: str with default "extra"?
FastAPI
from pydantic import BaseModel

class ExtendedModel(BaseModel):
    extra: str = "extra"
A
class ExtendedModel(BaseModel):
    extra = str("extra")
B
class ExtendedModel(BaseModel):
    extra str = "extra"
C
class ExtendedModel(BaseModel):
    extra: str "extra"
D
class ExtendedModel(BaseModel):
    extra: str = "extra"
Attempts:
2 left
💡 Hint
Check the correct syntax for type annotations in Python classes.
🔧 Debug
advanced
2:00remaining
Why does this inherited Pydantic model raise ValidationError?
Given these models, why does creating ChildModel(name=123) raise a ValidationError?
FastAPI
from pydantic import BaseModel

class ParentModel(BaseModel):
    name: str

class ChildModel(ParentModel):
    age: int = 10

child = ChildModel(name=123)
ABecause ChildModel cannot inherit from ParentModel
BBecause 'age' is missing and has no default value
CBecause 'name' is expected to be a string but an integer was provided
DBecause Pydantic models cannot have default values
Attempts:
2 left
💡 Hint
Check the type of the 'name' field and the value passed.
state_output
advanced
2:00remaining
Value of attribute after model inheritance and override
What is the value of child.name after running this code?
FastAPI
from pydantic import BaseModel

class ParentModel(BaseModel):
    name: str = "parent"

class ChildModel(ParentModel):
    name: str = "child"

child = ChildModel()
ARaises AttributeError
B"child"
CNone
D"parent"
Attempts:
2 left
💡 Hint
Child class fields override parent fields with the same name.
🧠 Conceptual
expert
3:00remaining
Effect of multiple inheritance on Pydantic model fields
Given these models, what fields does MultiChildModel have and what are their default values?
FastAPI
from pydantic import BaseModel

class ModelA(BaseModel):
    a: int = 1

class ModelB(BaseModel):
    b: int = 2

class MultiChildModel(ModelA, ModelB):
    c: int = 3

model = MultiChildModel()
AFields: a=1, b=2, c=3
BFields: b=2, c=3 (a is ignored)
CFields: a=1, c=3 (b is ignored)
DRaises TypeError due to multiple inheritance
Attempts:
2 left
💡 Hint
Pydantic supports multiple inheritance but only from BaseModel subclasses.