0Pricing
Flask Academy · 课时

定义您的第一个模型类

将 Python 类映射到数据库表

定义您的第一个模型类 是 CoddyKit 上的免费 Flask Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Flask Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Flask Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

A Model Is a Table

In the ORM world, one Python class equals one database table. Each model describes the shape of the rows it will store. 🧱

Inherit from db.Model

Your class subclasses db.Model. That single base class gives it table powers like querying and saving for free.

class User(db.Model):
    pass

Class Name to Table Name

By default the table name is the lowercase class name. So a class named User maps to a table called user automatically.

Columns Are Class Attributes

Each field is a class attribute set to db.Column. The column type, like Integer or String, tells the database what to store.

name = db.Column(db.String(80))

Every Table Needs a Key

One column should be the primary key: a unique id for each row. It is how the database tells your records apart.

id = db.Column(db.Integer, primary_key=True)

Your First Full Model

Put it together: a key plus a couple of fields. This tiny class fully describes a User table and its columns.

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))

Override the Table Name

Want a custom table name? Set __tablename__ on the class and the ORM uses that instead of the default.

__tablename__ = "users"

Create Instances Like Objects

A model instance is just a normal Python object. You pass column values as keyword arguments, and each becomes a row to save.

u = User(name="Ada")

Add a Helpful repr

Defining __repr__ makes objects print clearly in the shell and logs, which saves you guessing during debugging.

def __repr__(self):
    return f"<User {self.name}>"

Models Live in One Place

Keep models in a models.py file so they import cleanly into views, scripts, and tests without circular imports.

No SQL in Sight

Notice you never wrote CREATE TABLE. The model declaration is the schema, and the ORM translates it to SQL for you.

Quick Check

What does a Flask-SQLAlchemy model class represent?

Recap

You built a model by subclassing db.Model, declared columns, set a primary key, and learned that the class itself is your schema. ✅

常见问题解答

「定义您的第一个模型类」课时是免费的吗?

是的 — 「定义您的第一个模型类」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flask Academy 课程的其余内容,请升级到 CoddyKit PRO。 Flask Academy 课程共包含 4 节课。

「定义您的第一个模型类」这节课中我会学到什么?

将 Python 类映射到数据库表 你通过在浏览器中直接运行的动手代码来练习 Flask Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Flask Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Flask Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「定义您的第一个模型类」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Flask Academy 课中编写并运行代码吗?

能。每节 Flask Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 安装并配置扩展
  2. 定义您的第一个模型类
  3. 列、类型与约束
  4. 使用 create_all 创建模式
← 返回 Flask Academy