0Pricing
Django Academy · Lesson

values, values_list, and count

Pull lightweight data and aggregates.

values, values_list, and count is a free Django Academy lesson on CoddyKit — lesson 4 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.

Why Go Lightweight

Full model objects carry every field. When you only need a column or two, lighter tools fetch less data and run faster.

values() Returns Dicts

Call values() and each row comes back as a dictionary instead of a model instance. Handy for JSON and quick reads.

Book.objects.values("title", "year")

Pick Only What You Need

Name the fields you pass to values() and Django selects just those columns, keeping each dictionary small and focused.

Book.objects.values("title")

values_list() Returns Tuples

Prefer tuples? values_list() gives each row as a tuple of values in the order you named the fields.

Book.objects.values_list("title", "year")

Flatten a Single Column

Asking for one field? Add flat=True to get a clean list of plain values instead of one-item tuples.

Book.objects.values_list("title", flat=True)

flat Needs One Field

Remember, flat=True only works with a single field. Pass two fields and Django raises an error to keep things clear.

These Are Still QuerySets

Both values() and values_list() stay lazy QuerySets, so you can filter and order them before they ever run.

Book.objects.filter(published=True).values("title")

Count Without Loading

Need just a total? count() asks the database to count rows directly, far cheaper than pulling them all into Python.

Book.objects.filter(published=True).count()

count vs len

Use count() when you only need the number. Calling len() first loads every object, then counts in memory.

Does Anything Match

To check if rows exist, exists() beats count(). It stops at the first match and returns a simple True or False.

Book.objects.filter(year=2024).exists()

Combine for Clean Reads

Mix these freely: filter to narrow, then values_list for a tidy list or count for a total. Each query stays lean.

Book.objects.filter(published=True).values_list("id", flat=True)

Quick Check

You want a plain list of every book title, not tuples or dictionaries. What do you use?

Recap

Wonderful! values() gives dicts, values_list() gives tuples, and count() totals rows in the database. 📊

Frequently asked questions

Is the “values, values_list, and count” lesson free?

Yes — the full text of “values, values_list, and count” 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 “values, values_list, and count”?

Pull lightweight data and aggregates. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “values, values_list, and count” 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

  1. all(), get(), and filter()
  2. Field Lookups and exclude()
  3. order_by, Slicing, and Lazy Evaluation
  4. values, values_list, and count
← Back to Django Academy