นิพจน์ F สำหรับการอัปเดตแบบอะตอมิก
อัปเดตคอลัมน์โดยใช้ค่าของคอลัมน์นั้นเองอย่างปลอดภัย
นิพจน์ F สำหรับการอัปเดตแบบอะตอมิก เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Read-Modify-Write Trap
Loading a value, changing it in Python, then saving creates a race: two requests can read the same number and overwrite each other.
Enter F Expressions
An F expression refers to a column by name, letting the database do the math itself instead of round-tripping the value to Python.
from django.db.models import FIncrement in the Database
Use F to add to a column based on its own current value, computed safely inside a single SQL statement.
Product.objects.filter(id=1).update(stock=F("stock") - 1)Why This Is Atomic
The whole change runs as one UPDATE, so concurrent requests stack correctly instead of clobbering one another's results.
Update Many Rows at Once
Because F runs in SQL, you can apply a change across a whole queryset in one query, like giving every product a raise.
Product.objects.update(price=F("price") * 1.1)Compare Two Columns
F expressions also shine in filters, letting you compare one column against another directly in the query.
Order.objects.filter(shipped__gt=F("ordered"))Refresh After update()
An in-memory instance does not change when you run update, so call refresh_from_db to pull the new value back.
p.refresh_from_db()F on a Single Instance
You can also assign an F expression to a field and then save; Django turns it into the same column-aware SQL.
p.stock = F("stock") - 1
p.save()Arithmetic Across Fields
F values support full arithmetic, so you can combine columns, for example storing a total computed from price and quantity.
Line.objects.update(total=F("price") * F("qty"))A Common Gotcha
After saving with an F expression, the Python attribute still holds the expression object, not the number, until you refresh.
When to Reach for F
Pick F whenever an update depends on a column's current value, especially counters, balances, and stock that many requests touch.
Quick Check
Two checkouts hit the same product at once. Which approach avoids losing a decrement?
Recap
F expressions push math into the database for atomic, race-free updates across one or many rows. Just refresh your instance afterward. 💾
คำถามที่พบบ่อย
บทเรียน “นิพจน์ F สำหรับการอัปเดตแบบอะตอมิก” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “นิพจน์ F สำหรับการอัปเดตแบบอะตอมิก” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “นิพจน์ F สำหรับการอัปเดตแบบอะตอมิก”
อัปเดตคอลัมน์โดยใช้ค่าของคอลัมน์นั้นเองอย่างปลอดภัย คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “นิพจน์ F สำหรับการอัปเดตแบบอะตอมิก” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม
ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- aggregate เทียบกับ annotate
- นิพจน์ F สำหรับการอัปเดตแบบอะตอมิก
- ออบเจ็กต์ Q สำหรับตัวกรองซับซ้อน
- การรวมแบบมีเงื่อนไขด้วย Case/When