0
0
FastAPIframework~10 mins

Response model exclude and include 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 FastAPI endpoint that returns a User model excluding the password field.

FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class User(BaseModel):
    username: str
    email: str
    password: str

@app.get("/user", response_model=User, response_model_exclude=[1])
def get_user():
    return User(username="alice", email="alice@example.com", password="secret")
Drag options to blanks, or click blank then click option'
Apassword
B{'password'}
C['password']
Dpasswords
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list instead of a set for response_model_exclude.
Passing a string without quotes or as a variable name.
Misspelling the field name.
2fill in blank
medium

Complete the code to include only the username and email fields in the response model.

FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class User(BaseModel):
    username: str
    email: str
    password: str

@app.get("/user", response_model=User, response_model_include=[1])
def get_user():
    return User(username="bob", email="bob@example.com", password="hidden")
Drag options to blanks, or click blank then click option'
Ausername, email
B['username', 'email']
Cusername email
D{'username', 'email'}
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list instead of a set for response_model_include.
Passing a string without quotes or as a variable name.
Including fields not defined in the model.
3fill in blank
hard

Fix the error in the code to exclude the email field from the response model.

FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class User(BaseModel):
    username: str
    email: str
    password: str

@app.get("/user", response_model=User, response_model_exclude=[1])
def get_user():
    return User(username="carol", email="carol@example.com", password="topsecret")
Drag options to blanks, or click blank then click option'
A{'email'}
B['email']
Cemail
D('email')
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list instead of a set.
Passing a string without quotes.
Using parentheses which create a string, not a tuple or set.
4fill in blank
hard

Fill both blanks to include only username and exclude password in the response model.

FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class User(BaseModel):
    username: str
    email: str
    password: str

@app.get("/user", response_model=User, response_model_include=[1], response_model_exclude=[2])
def get_user():
    return User(username="dave", email="dave@example.com", password="hiddenpass")
Drag options to blanks, or click blank then click option'
A{'username'}
B{'email'}
C{'password'}
D{'username', 'email'}
Attempts:
3 left
💡 Hint
Common Mistakes
Using lists instead of sets.
Including fields not needed.
Confusing include and exclude sets.
5fill in blank
hard

Fill all three blanks to include username and email, exclude password, and return the user data.

FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class User(BaseModel):
    username: str
    email: str
    password: str

@app.get("/user", response_model=User, response_model_include=[1], response_model_exclude=[2])
def get_user():
    return User(username=[3], email="eve@example.com", password="secret")
Drag options to blanks, or click blank then click option'
A{'username', 'email'}
B{'password'}
C"eve"
D"eve123"
Attempts:
3 left
💡 Hint
Common Mistakes
Using lists instead of sets for include/exclude.
Not quoting the username string.
Excluding wrong fields.