Field Lookups and exclude()
Query with contains, gte, in, and more.
Field Lookups and exclude() is a free Django Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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. 🔍
Frequently asked questions
Is the “Field Lookups and exclude()” lesson free?
Yes — the full text of “Field Lookups and exclude()” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.
What will I learn in “Field Lookups and exclude()”?
Query with contains, gte, in, and more. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Django Academy?
No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Field Lookups and exclude()” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Django Academy lesson?
Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- all(), get(), and filter()
- Field Lookups and exclude()
- order_by, Slicing, and Lazy Evaluation
- values, values_list, and count