DRF +
pip install drf-spectacular. Do not look at solutions.md before submitting.
Exercise 1 — The serializer with limits
- Write the full
ReserveInputSerializer(event UUID, seats 1-8 with sector/row/number, optional write_only promo_code). - Test from the shell with
serializer.is_valid(): 0 seats, 9 seats, malformed seats, nonexistent uuid. Useful messages in every case? - Add
validate_eventchecking existence (shape/reference) and justify why "past event" (business) goes in the service and not here.
Exercise 2 — The layers in action
- Send a POST with malformed JSON, another with wrong types and another with impossible business (seat taken). Document which layer answers each and with which status.
- Move (hypothetically) the "seat free" check into the serializer: which race do you reintroduce? (connect it with 10).
Exercise 3 — The living contract
- Install drf-spectacular, publish
/api/schema/and/api/docs/. Download the YAML and review what it guessed wrong (undocumented responses, ambiguous request bodies). - Annotate
@extend_schemaon your 3 most complex endpoints (reserve, webhook, card redeem) with documented request/responses/errors. - Add
spectacular --fail-on-warnto the pipeline (or a local script) and make it pass.
Exercise 4 — The contract as a test
- Write a test that: calls your reserve endpoint, takes the JSON response and validates that its status/structure matches the schema's declaration (minimum: status code and body keys).
- Extract the schema's
examplesand validate a real response against the example (by hand or with jsonschema). How many times have you seen docs that lie? This test is the vaccine.
Submit
Paste the serializer, the trimmed schema and the CI results. Next: Lesson 16 — gRPC, GraphQL, WebSockets/SSE.