Chapter 1: QuerySet Introduction

QuerySet Introduction

QuerySets — the thing you get when you write Question.objects.all(), .filter(), .order_by(), etc.

Most beginners treat QuerySets like “just a list of objects” — they loop over them, slice them, print them, and think “okay, it’s a list”. But QuerySets are not lists. They are lazy database query builders — very clever, chainable, SQL-optimizing promises that don’t actually hit the database until you force them to.

Understanding QuerySets properly is the single biggest thing that separates “Django code that works” from “Django code that is fast, clean, scalable, and professional”.

We are going to learn this slowly, step-by-step, like pair-programming — I’ll explain every line, when SQL runs, what happens in memory, common traps, and the patterns you’ll use 50 times a day in real projects.

We’ll do almost everything in the Django shell so you see exactly what’s happening.

Step 1: Open the Shell & Import Models

Bash
Python

Assume you already have some questions and choices in the database (from admin or earlier shell sessions).

Step 2: The Most Basic QuerySet – .all()

Python

Important facts:

  • qs is not a list — it is a QuerySet object
  • No SQL has run yet — Django has only prepared the query
  • You can print it → it forces evaluation → now SQL runs: SELECT * FROM polls_question
Python

Key takeaway: QuerySets are lazy — they delay database access until you force them (loop, len, list(), print, indexing, etc.).

Step 3: Chaining – The Real Magic

Every method that filters / orders / annotates returns a new QuerySet — you can chain forever:

Python

This generates one efficient SQL query:

SQL

Important: chaining is immutable — qs.filter() returns new object — original qs unchanged

Step 4: Most Common Methods You Will Use Every Day

Python

Step 5: Aggregates & Annotations (Very Powerful)

Python
Python

Step 6: Avoiding N+1 Queries (The #1 Performance Killer)

Bad (slow – N+1 problem):

Python

Good:

Python
Python

Step 7: Your Quick Practice Task (Do This in Shell Now)

  1. from polls.models import Question, Choice
  2. qs = Question.objects.filter(is_active=True).order_by(“-pub_date”)[:5]
  3. print(qs) → see objects
  4. qs.count() → fast count
  5. qs.annotate(vote_count=Sum(“choices__votes”)) → add vote_count
  6. for q in qs: print(q.question_text, q.vote_count)
  7. Try qs.filter(pub_date__year=2026)

Tell me what feels next:

  • Which QuerySet part is still confusing? (chaining? lookups? N+1? annotation?)
  • Want 20 more real-life QuerySet examples from different scenarios?
  • Ready to learn Q objects (complex OR conditions)?
  • Or finally ready for Forms + Voting + POST + F() expression?

You’re now starting to think like Django ORM pros — this QuerySet mastery is the single biggest skill upgrade after models themselves.

Keep playing in shell — you’re doing fantastic! 🚀🇮🇳

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *