Software Engineer (SDE)fastapibeginner3 interactive MCQs in app
Response Models, Status Codes & JSONResponse
response_model data filtering & secret strippingsemantic HTTP 201 / 202 / 204 status codesORM model_config = ConfigDict(from_attributes=True)
Core Concept
Setting `response_model=PublicUserOut` and `status_code=status.HTTP_201_CREATED` filters, validates, and documents outgoing responses—**stripping sensitive DB fields (`password_hash`, `expected_answer`, `correct_option_index`) automatically before JSON serialization!** Never return raw ORM/DB dicts directly when internal columns exist. Return explicit `201 Created` on resource creation, `204 No Content` (`Response(status_code=204)`) on deletes, and `202 Accepted` on async evaluation jobs!
Worked Example
`@app.post("/users", response_model=PublicUser, status_code=201)` automatically removes `password_hash` from the returned DB row before sending JSON!
Common Pitfall
omitting `response_model` leaks server secrets/columns added later in DB migrations straight to clients!
Recommended Readings & Practice
Practice Response Models, Status Codes & JSONResponse with 3 graded MCQs & written feedback.
Take SOARDO’s free 12-question diagnostic to see how strong you are across your target role and unlock adaptive daily missions.