0Pricing
Django Academy · บทเรียน

อ่านไฟล์การย้ายโครงสร้าง

ทำความเข้าใจการดำเนินการและสิ่งที่ต้องพึ่งพาภายในไฟล์

อ่านไฟล์การย้ายโครงสร้าง เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

It Is Just Python

A migration is an ordinary Python file you can open and read. Nothing magic lives inside, just a class describing changes.

The Migration Class

Every file defines a class named Migration that subclasses migrations.Migration. Django finds it by that name.

class Migration(migrations.Migration):
    ...

Dependencies First

The dependencies list tells Django which migrations must run before this one, forming a clear order.

dependencies = [
    ("blog", "0001_initial"),
]

The operations List

The heart of the file is operations, a list of steps Django runs in order to change your schema.

operations = [
    migrations.AddField(...),
]

CreateModel

The first migration usually holds a CreateModel operation, which builds a brand-new table from your model.

migrations.CreateModel(
    name="Post",
    fields=[...],
)

AddField

AddField appears when you add a column to an existing model, naming the model, the field, and its type.

migrations.AddField(
    model_name="post",
    name="views",
    field=models.IntegerField(default=0),
)

Other Operations

You will also meet RemoveField, AlterField, and RenameField. Each maps to one clear change in the database.

initial = True

The first migration of an app sets initial to True. It marks the starting point of that app history.

initial = True

Numbered Filenames

Files are numbered like 0001, 0002, and so on. The number plus the dependencies define the exact apply order.

Read, Do Not Hand-Edit

Read migrations to understand a change, but let Django generate them. Hand-editing is rare and easy to get wrong.

It Tells a Story

Reading files in order replays your schema history: each one is a small, reviewable chapter of how tables grew.

Quick Check

What does the dependencies list in a migration do?

Recap

A migration is plain Python: a Migration class with dependencies and an operations list. Now you can read any file and follow the change. 📖

คำถามที่พบบ่อย

บทเรียน “อ่านไฟล์การย้ายโครงสร้าง” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “อ่านไฟล์การย้ายโครงสร้าง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “อ่านไฟล์การย้ายโครงสร้าง”

ทำความเข้าใจการดำเนินการและสิ่งที่ต้องพึ่งพาภายในไฟล์ คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “อ่านไฟล์การย้ายโครงสร้าง” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม

ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. makemigrations กับ migrate
  2. อ่านไฟล์การย้ายโครงสร้าง
  3. เปลี่ยนโมเดลและย้ายโครงสร้างอีกครั้ง
  4. sqlmigrate และการตรวจสอบ SQL
← กลับไปที่ Django Academy