Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The response_model_exclude parameter expects a set of field names to exclude. Using {'password'} correctly excludes the password field.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
response_model_include expects a set of field names to include. Using {'username', 'email'} correctly includes only those fields.
3fill in blank
hardFix 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'
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.
✗ Incorrect
response_model_exclude requires a set of field names. Using {'email'} correctly excludes the email field.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lists instead of sets.
Including fields not needed.
Confusing include and exclude sets.
✗ Incorrect
To include only username, use {'username'} for response_model_include. To exclude password, use {'password'} for response_model_exclude.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lists instead of sets for include/exclude.
Not quoting the username string.
Excluding wrong fields.
✗ Incorrect
Include username and email with {'username', 'email'}, exclude password with {'password'}, and set username to "eve".