0
0
FastAPIframework~10 mins

Model inheritance 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 base Pydantic model class.

FastAPI
from pydantic import BaseModel

class UserBase([1]):
    username: str
    email: str
Drag options to blanks, or click blank then click option'
ABaseModel
BModel
CBase
DModelBase
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Model' or 'ModelBase' which are not valid Pydantic base classes.
Using 'Base' alone which is incomplete.
2fill in blank
medium

Complete the code to create a model that inherits from UserBase and adds an id field.

FastAPI
class UserCreate(UserBase):
    id: [1]
Drag options to blanks, or click blank then click option'
Abool
Bstr
Cint
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using str for id which is usually numeric.
Using float or bool which are not typical for IDs.
3fill in blank
hard

Fix the error in the model inheritance syntax.

FastAPI
class UserResponse([1]):
    id: int
    username: str
    email: str
Drag options to blanks, or click blank then click option'
AUserBase()
BBaseModel()
CBaseModel
DUserBase
Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses after the base class name in class definition.
Using the wrong base class.
4fill in blank
hard

Fill both blanks to create a model that inherits from UserBase and adds an optional age field.

FastAPI
from typing import [1]

class UserProfile([2]):
    age: [1][int] = None
Drag options to blanks, or click blank then click option'
AOptional
BUserBase
CDict
DSet
Attempts:
3 left
💡 Hint
Common Mistakes
Using List or Dict instead of Optional for optional fields.
Not inheriting from UserBase.
5fill in blank
hard

Fill all three blanks to create a model that inherits from UserBase, adds a list of tags, and a method to return username in uppercase.

FastAPI
from typing import [1]

class UserAdvanced([2]):
    tags: [1][str] = []

    def get_upper_username(self) -> str:
        return self.username.[3]()
Drag options to blanks, or click blank then click option'
AList
BUserBase
Cupper
DOptional
Attempts:
3 left
💡 Hint
Common Mistakes
Using Optional instead of List for tags.
Not inheriting from UserBase.
Using wrong string method like 'capitalize' instead of 'upper'.