Django Academy · Lektion

F Expressions für atomare Aktualisierungen

Spalten sicher anhand ihrer eigenen Werte aktualisieren

Lektion 2 von 413 Schritte

F Expressions für atomare Aktualisierungen ist eine kostenlose Django Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Django Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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 F

Increment 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. 💾

Kostenlos starten

Lerne Python mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
30
Lektionen
120

Häufig gestellte Fragen

Ist die Lektion „F Expressions für atomare Aktualisierungen“ kostenlos?

Ja — der vollständige Text von „F Expressions für atomare Aktualisierungen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Django Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „F Expressions für atomare Aktualisierungen“?

Spalten sicher anhand ihrer eigenen Werte aktualisieren Du übst Django Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Django Academy zu starten?

Keine Vorkenntnisse erforderlich. Django Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „F Expressions für atomare Aktualisierungen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Django Academy-Lektion Code schreiben und ausführen?

Ja. Jede Django Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. aggregate vs. annotate
  2. F Expressions für atomare Aktualisierungen
  3. Q Objects für komplexe Filter
  4. Bedingte Aggregation mit Case/When
← Zurück zu Django Academy