กำหนดคลาสโมเดลแรกของคุณ
แมปคลาส Python เข้ากับตารางฐานข้อมูล
กำหนดคลาสโมเดลแรกของคุณ เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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):
passClass 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “กำหนดคลาสโมเดลแรกของคุณ”
แมปคลาส Python เข้ากับตารางฐานข้อมูล คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “กำหนดคลาสโมเดลแรกของคุณ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม
ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ติดตั้งและกำหนดค่าส่วนขยาย
- กำหนดคลาสโมเดลแรกของคุณ
- คอลัมน์ ชนิดข้อมูล และข้อจำกัด
- สร้างแบบแผนด้วย create_all