Feld-Lookups und exclude()
Fragen Sie mit contains, gte, in und weiteren Lookups ab.
Feld-Lookups und exclude() 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.
Beyond Equality
Plain filters check for equality, but real queries need ranges and text matches. Field lookups unlock that extra power.
The Double Underscore
A lookup is the field name, a double underscore, then the operator. That tiny syntax is how Django reads richer conditions.
Book.objects.filter(year__gte=2020)Greater and Less Than
Use gt, gte, lt, and lte for numeric or date ranges. They map straight to SQL comparison operators.
Book.objects.filter(price__lt=20)Search Inside Text
The contains lookup finds rows where a field holds your substring anywhere inside it. Great for simple searches.
Book.objects.filter(title__contains="Python")Ignore the Case
Add an i for case-insensitive text lookups like icontains or iexact, so capitals never trip up a match.
Book.objects.filter(title__icontains="python")Starts and Ends
Match the edges of a string with startswith and endswith. Both have case-insensitive twins too.
Book.objects.filter(title__startswith="The")Match a Set of Values
The in lookup keeps rows whose value appears in a list you pass. It is cleaner than chaining many OR checks.
Book.objects.filter(year__in=[2022, 2023, 2024])Check for Empty Values
Use isnull to find rows where a column is NULL, or set it False to find the rows that have a value.
Book.objects.filter(summary__isnull=True)Flip the Logic
Sometimes you want everything except a match. exclude() returns the rows that do NOT meet the condition.
Book.objects.exclude(published=True)Lookups Work in exclude Too
Any field lookup that works in filter also works in exclude(). The only difference is the result is inverted.
Book.objects.exclude(year__gte=2020)Combine filter and exclude
Chain both together to keep one group and drop another. Each call narrows the same lazy QuerySet further.
Book.objects.filter(published=True).exclude(price__gt=50)Quick Check
You want books whose title holds "django" regardless of capitalization. Which lookup fits?
Recap
You leveled up! Field lookups use the double underscore for ranges and text, and exclude() flips any condition. 🔍
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 „Feld-Lookups und exclude()“ kostenlos?
Ja — der vollständige Text von „Feld-Lookups und exclude()“ 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 „Feld-Lookups und exclude()“?
Fragen Sie mit contains, gte, in und weiteren Lookups ab. 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 „Feld-Lookups und exclude()“?
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
- all(), get() und filter()
- Feld-Lookups und exclude()
- order_by, Slicing und Lazy Evaluation
- values, values_list und count