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

นำเข้าและตรวจสอบโมเดล

นำเข้าโมเดลและดูฟิลด์ของโมเดล

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

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

Bring Your Models In

Before you can query data, you import the model class into the shell. Pull it from the app where you defined it.

>>> from blog.models import Post

The App Path Pattern

The import path is always appname.models. So a Post in the blog app lives at blog.models, matching your folder layout.

Confirm It Imported

Type the class name alone to see Django confirm the model. It echoes the class so you know the import worked.

>>> Post
<class 'blog.models.Post'>

Every Model Has a Manager

Each model carries a manager called objects. It is your gateway to every query, like fetching or counting rows.

>>> Post.objects

Peek at the Fields

To list a model's columns, read its hidden _meta object. It exposes the fields Django mapped to your table.

>>> Post._meta.get_fields()

Field Names at a Glance

A quick comprehension turns that into a clean list of field names, handy when you forget a column.

>>> [f.name for f in Post._meta.get_fields()]

Inspect One Field

You can grab a single field by name with get_field. It returns the field object so you can read its details.

>>> Post._meta.get_field('title')

Read a Field's Type

Ask a field for its type with type(). This tells you whether a column is a CharField, IntegerField, and so on.

>>> type(Post._meta.get_field('title'))

Use Python's help

Plain Python tools still work. Run help on the model to read its docstring and see available attributes.

>>> help(Post)

Import More Than One

Need several models? Import them together on one line so you can cross-reference data without retyping.

>>> from blog.models import Post, Comment

Inspect Before You Query

Inspecting first saves time. Knowing the exact field names prevents typos when you start filtering real records.

Quick Check

How do you list the fields a model defines?

Recap

You imported models with appname.models, met the objects manager, and used _meta to inspect fields before querying. 🔍

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

บทเรียน “นำเข้าและตรวจสอบโมเดล” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “นำเข้าและตรวจสอบโมเดล” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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. เปิด python manage.py shell
  2. นำเข้าและตรวจสอบโมเดล
  3. สร้างและบันทึกออบเจ็กต์แบบโต้ตอบ
  4. shell_plus และเครื่องมือที่ดียิ่งขึ้น
← กลับไปที่ Django Academy