تعبيرات F للتحديثات الذرية
تحديث الأعمدة بأمان باستخدام قيمها الخاصة
تعبيرات F للتحديثات الذرية درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 للتحديثات الذرية» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «تعبيرات F للتحديثات الذرية»؟
تحديث الأعمدة بأمان باستخدام قيمها الخاصة تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «تعبيرات F للتحديثات الذرية»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- aggregate مقابل annotate
- تعبيرات F للتحديثات الذرية
- كائنات Q لعوامل التصفية المعقدة
- التجميع الشرطي باستخدام Case/When