0Pricing
Flask Academy · Lesson

Define a Schema for a Model

Declare fields that map to your model.

Define a Schema for a Model is a free Flask Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What a Schema Is

A Schema is a Python class that lists the fields your data should have. It mirrors a model but lives separately, just for input and output.

Subclass Schema

Every schema inherits from Marshmallow's base Schema class. Naming it after your model, like UserSchema, keeps the intent obvious at a glance.

class UserSchema(Schema):
    pass

Declare Fields

Inside the class you add attributes from the fields module. Each one names a key that will appear in your serialized JSON output.

id = fields.Int()
name = fields.Str()

Field Types Matter

Pick a field type that matches the data: Int, Str, Bool, Float, or DateTime. The type controls how values are formatted and validated.

active = fields.Bool()
score = fields.Float()

Dates Done Right

A DateTime field serializes Python datetimes into clean ISO strings automatically. No more manual formatting scattered across your views.

created_at = fields.DateTime()

Mark Required Fields

Pass required=True to a field so loading fails if it is missing. This is your first taste of validation baked right into the schema.

email = fields.Str(required=True)

Leave Secrets Out

Only list fields you want to expose. By simply not declaring password_hash, the schema can never leak it into a response.

Rename with data_key

Want camelCase JSON from a snake_case attribute? Use data_key to map an internal name to a different external key.

first_name = fields.Str(data_key="firstName")

dump_only Fields

Some fields, like an id, should appear in output but never be accepted as input. Mark them dump_only to keep them read-only.

id = fields.Int(dump_only=True)

Instantiate the Schema

You create one instance to reuse: user_schema equals UserSchema(). Pass many=True later when you serialize a whole list.

user_schema = UserSchema()

One Schema, Two Jobs

This single class will both dump models to JSON and load JSON back into checked data. You define the shape once and use it both directions.

Quick Check

You want id readable in output but never accepted as input.

Recap

A Schema subclass declares typed fields that map to your model. Options like required, dump_only, and data_key shape exactly what goes in and out.

Frequently asked questions

Is the “Define a Schema for a Model” lesson free?

Yes — the full text of “Define a Schema for a Model” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.

What will I learn in “Define a Schema for a Model”?

Declare fields that map to your model. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Flask Academy?

No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Define a Schema for a Model” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Flask Academy lesson?

Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Hand-Built JSON Falls Apart
  2. Define a Schema for a Model
  3. Dump Objects to JSON
  4. Load and Validate Input Payloads
← Back to Flask Academy