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

อัปเดตระเบียนเดิมอย่างปลอดภัย

เปลี่ยนฟิลด์และส่งการเปลี่ยนแปลงอย่างถูกต้อง

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

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

The U in CRUD

Updating means changing an existing row, not making a new one. The trick is to load the record first, then edit it. ✏️

Find Before You Change

Always fetch the target row by its id before touching it. get_or_404() loads it or stops with a clean 404.

user = User.query.get_or_404(user_id)

Edit Like a Python Object

Once loaded, just reassign its attributes. SQLAlchemy notices the change and marks the object as dirty for you.

user.name = "New Name"

Read the New Values

Updates usually carry the new data in the request body. Parse it with get_json() before applying anything.

data = request.get_json()

Apply Fields Carefully

Only overwrite fields the client actually sent. Using get() with a fallback avoids wiping values to None by accident.

user.name = data.get("name", user.name)

Commit the Change

No add() is needed for an existing row. A single commit() flushes your edits to the database.

db.session.commit()

PUT Replaces, PATCH Tweaks

Use PUT when the client sends the whole object and PATCH when it sends only the fields it wants to change.

@app.route("/users/<int:id>", methods=["PATCH"])

Validate Before Saving

Check incoming values before you commit. Reject bad data with a 400 so you never persist a broken record.

if not data.get("name"):
    return jsonify(error="name required"), 400

Roll Back if It Breaks

If the commit raises, call rollback() to discard the half-applied edit and leave the row exactly as it was.

except Exception:
    db.session.rollback()

Return the Updated Row

A good update reply sends back the fresh record with status 200, so the client sees the saved result.

return jsonify({"id": user.id, "name": user.name}), 200

Load, Edit, Commit

The whole update flow is three beats: load the row, change its attributes, then commit. No new object ever appears.

Quick Check

You changed a loaded object's attribute. What must happen for the edit to stick?

Recap: Updating Records

You load with get_or_404, apply only sent fields, validate, then commit, rolling back on errors. Safe updates unlocked! 🎉

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

บทเรียน “อัปเดตระเบียนเดิมอย่างปลอดภัย” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “อัปเดตระเบียนเดิมอย่างปลอดภัย”

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

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

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

บทเรียน “อัปเดตระเบียนเดิมอย่างปลอดภัย” ใช้เวลานานแค่ไหน

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

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

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

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

  1. สร้างระเบียนและยืนยันเซสชัน
  2. ปลายทางอ่านหนึ่งรายการและอ่านหลายรายการ
  3. อัปเดตระเบียนเดิมอย่างปลอดภัย
  4. ลบและจัดการแถวที่หายไป
← กลับไปที่ Flask Academy