Pybites Logo

Model Inheritance and API Schemas

Level: Intermediate (score: 3)

You're about to build a REST API for the gym app. There's a problem: APIs need different shapes of data for different operations.

When creating a workout, you don't send an id (database generates it). When reading a workout, you want the id included. When returning a workout with exercises, you want nested data.

Instead of duplicating fields, you'll use model inheritance to share common fields and create specialized schemas for different use cases.