Bird
Raised Fist0
FastAPIframework~20 mins

Model inheritance in FastAPI - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of model inheritance in FastAPI?
easy
A. To reuse fields and validation rules from a parent model in child models
B. To create unrelated models with no shared fields
C. To automatically generate database tables
D. To handle HTTP requests directly

Solution

  1. Step 1: Understand model inheritance concept

    Model inheritance allows child models to reuse fields and validation from a parent model, avoiding repetition.
  2. Step 2: Identify the purpose in FastAPI context

    FastAPI uses Pydantic models where inheritance helps keep code clean by extending existing models.
  3. Final Answer:

    To reuse fields and validation rules from a parent model in child models -> Option A
  4. Quick Check:

    Model inheritance = reuse fields and validation [OK]
Hint: Inheritance means child models get parent fields automatically [OK]
Common Mistakes:
  • Thinking inheritance creates database tables automatically
  • Confusing model inheritance with HTTP request handling
  • Believing inheritance creates unrelated models
2. Which of the following is the correct way to inherit a Pydantic model in FastAPI?
easy
A. class ChildModel inherits ParentModel: pass
B. class ChildModel extends ParentModel: pass
C. class ChildModel(ParentModel): pass
D. class ChildModel : ParentModel

Solution

  1. Step 1: Recall Python class inheritance syntax

    In Python, inheritance is done by placing the parent class name in parentheses after the child class name.
  2. Step 2: Match with FastAPI model inheritance

    FastAPI uses Pydantic models which follow Python syntax, so class ChildModel(ParentModel): pass is correct.
  3. Final Answer:

    class ChildModel(ParentModel): pass -> Option C
  4. Quick Check:

    Python inheritance syntax = parentheses [OK]
Hint: Use parentheses for inheritance in Python classes [OK]
Common Mistakes:
  • Using 'extends' like in JavaScript or Java
  • Using 'inherits' keyword which doesn't exist in Python
  • Using colon without parentheses for inheritance
3. Given the following code, what fields will ChildModel have?
from pydantic import BaseModel

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

class ChildModel(ParentModel):
    age: int
medium
A. age only
B. id, name, age
C. id, name
D. name, age only

Solution

  1. Step 1: Identify fields in ParentModel

    ParentModel has fields id and name.
  2. Step 2: Identify fields added in ChildModel

    ChildModel inherits ParentModel and adds age.
  3. Final Answer:

    id, name, age -> Option B
  4. Quick Check:

    ChildModel fields = parent fields + new fields [OK]
Hint: Child model fields = parent fields + added fields [OK]
Common Mistakes:
  • Ignoring inherited fields from parent
  • Assuming child model only has new fields
  • Confusing field names or missing fields
4. Which of the following correctly describes this FastAPI model inheritance code?
from pydantic import BaseModel

class ParentModel(BaseModel):
    id: int

class ChildModel(ParentModel):
    id: str
medium
A. Field names must be unique across all models
B. ParentModel must inherit from ChildModel
C. BaseModel cannot be inherited
D. ChildModel can override a field with a different type

Solution

  1. Step 1: Check field types in parent and child

    ParentModel has id as int, ChildModel overrides id as str.
  2. Step 2: Understand Pydantic field overriding rules

    Pydantic allows overriding the type of an inherited field; the child's definition takes precedence.
  3. Final Answer:

    ChildModel can override a field with a different type -> Option D
  4. Quick Check:

    Field type override allowed [OK]
Hint: Inherited fields can be overridden with different types [OK]
Common Mistakes:
  • Thinking child cannot change field types
  • Confusing inheritance direction
  • Believing BaseModel cannot be inherited
5. You want to create a UserUpdate model that inherits from UserBase but makes all fields optional for partial updates. Which approach correctly uses model inheritance in FastAPI?
hard
A. class UserUpdate(UserBase): id: Optional[int] = None name: Optional[str] = None
B. class UserUpdate(UserBase): pass
C. class UserUpdate(BaseModel): id: int name: str
D. class UserUpdate(UserBase): id: int name: str

Solution

  1. Step 1: Understand the goal of partial updates

    Partial updates require fields to be optional so they can be omitted.
  2. Step 2: Use inheritance and make fields optional

    Inheriting from UserBase and redefining fields as Optional with default None allows partial updates.
  3. Final Answer:

    class UserUpdate(UserBase): id: Optional[int] = None name: Optional[str] = None -> Option A
  4. Quick Check:

    Make fields optional in child model for partial updates [OK]
Hint: Override fields as Optional with default None for partial updates [OK]
Common Mistakes:
  • Leaving fields required in update model
  • Not inheriting from base model
  • Copying fields without making them optional
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard