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

ดึงแถวด้วย query และ get

เรียกข้อมูลทั้งหมด หนึ่งรายการ หรือด้วยคีย์หลัก

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

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

Reading Starts With query

Every read from your database begins with the model's query attribute. It is your gateway to fetching rows as Python objects. 🔎

rows = User.query

Grab Everything with all

Call all() on a query to load every matching row into a plain Python list you can loop over.

users = User.query.all()
for u in users:
    print(u.name)

Just One With first

Use first() when you want a single row. It returns the first match, or None if the table has nothing to give.

user = User.query.first()

Fetch by Primary Key

When you know the id, get() is the fastest path. Hand it the primary key and it returns that exact row.

user = User.query.get(7)

get Returns None When Missing

If no row has that id, get() quietly returns None instead of raising. Always check before you use the result.

user = User.query.get(999)
if user is None:
    print("not found")

404 the Easy Way

In a view, get_or_404() fetches by id and auto-aborts with a 404 page when the row is absent. No manual check needed.

user = User.query.get_or_404(7)

first_or_404 for Filtered Reads

Pair first_or_404() with a filter to grab one match or raise a clean 404 when nothing fits.

u = User.query.filter_by(email=e).first_or_404()

Count Without Loading Rows

Need a tally, not the data? count() asks the database for the number directly, so you never pull full rows into memory.

total = User.query.count()

Queries Are Lazy

Building a query does not touch the database yet. The SQL only runs when you call a method like all, first, or get.

q = User.query
results = q.all()

Each Row Is an Object

Results come back as real model instances, so you read columns as ordinary attributes like user.name and user.email.

user = User.query.first()
print(user.email)

Pick the Right Fetch

Use get for a known id, first for one of many, and all for the whole set. Choosing well keeps reads clear and fast.

Quick Check

You know a record's primary key. Which call fetches it most directly?

Recap: Fetching Rows

You now read data with query: all for lists, first for one, get by id, and the or_404 helpers for clean views. Nice work! 🎉

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

บทเรียน “ดึงแถวด้วย query และ get” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “ดึงแถวด้วย query และ get”

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

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

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

บทเรียน “ดึงแถวด้วย query และ get” ใช้เวลานานแค่ไหน

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

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

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

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

  1. ดึงแถวด้วย query และ get
  2. กรอง เรียงลำดับ และจำกัดผลลัพธ์
  3. กำหนดความสัมพันธ์และคีย์ต่างประเทศ
  4. การรวมข้อมูลและการโหลดแบบช้ากับแบบเร็ว
← กลับไปที่ Flask Academy